laravel-5.4 - error :Creating default object from empty value(laravel-5.4 - 错误:从空值创建默认对象)
问题描述
我想在数据库中存储图像路径.vendorlaravelframeworksrcIlluminateFoundationAuthRegistersUsers.php
下的我的控制器代码如下:
I want to store image path in database. My Controller code undervendorlaravelframeworksrcIlluminateFoundationAuthRegistersUsers.php
follows:
public function register(Request $request)
{
$this->validator($request->all())->validate();
if($request->hasFile('image')) {
$image_name = $request->file('image')->getClientOriginalName();
$image_path = $request->file('image')->store('public');
$user->image = Storage::url($image_name);
$user->save();
}
event(new Registered($user = $this->create($request->all())));
// $this->guard()->login($user); disable autologin for new users
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
我收到以下错误:RegistersUsers.php 第 40 行中的 ErrorException:从空值创建默认对象 .
I am getting the following error: ErrorException in RegistersUsers.php line 40: Creating default object from empty value .
图像正在保存在公共文件夹中,但无法将图像路径保存在用户表数据库中.用户表的架构Schema::create('users', function (Blueprint $table) {$table->increments('user_id');$table->string('name');$table->string('image');$table->rememberToken();$table->timestamps();
The image is getting saved in public folder but not able to save image path in the user table database. schema of user table
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('name');
$table->string('image');
$table->rememberToken();
$table->timestamps();
如何将图像路径存储在数据库中的用户表中
how to proceed to store the image path in the user table in database
推荐答案
你还没有初始化变量 $user
所以在控制器中,在使用它之前你必须添加 $user = new User;
假设你已经有 use AppUser;
you haven't initialized the variable $user
so in the controller and before using it you've to add $user = new User;
assuming that you already have use AppUser;
或 $user = new AppUser;
如果你不这样做.
or $user = new AppUser;
if you don't.
更新如果您尝试更新现有的用户记录,您必须通过根据应在请求中发送的主键进行选择来初始化 $user
变量,然后进行如下初始化:>
Update
if you're trying to update an existing user record you've to initialize the $user
variable by selecting based on it's primary key which should be sent in the request and then doing the initialization like:
$user_id = $request->input('user_id');
$user = User::find($user_id);
这篇关于laravel-5.4 - 错误:从空值创建默认对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:laravel-5.4 - 错误:从空值创建默认对象
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01