php laravel-5.4 - 错误:从空值创建默认对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43488754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel-5.4 - error :Creating default object from empty value
提问by dynamic
I want to store image path in database. My Controller code undervendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php
follows:
我想在数据库中存储图像路径。我的控制器代码vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php
如下:
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());
}
I am getting the following error: ErrorException in RegistersUsers.php line 40: Creating default object from empty value .
我收到以下错误: ErrorException in RegistersUsers.php line 40: Creating default object from empty value 。
The image is getting saved in public folder but not able to save image path in the user table database. schema of user tableSchema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('name');
$table->string('image');
$table->rememberToken();
$table->timestamps();
图像正在保存在公共文件夹中,但无法将图像路径保存在用户表数据库中。用户表的模式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
如何继续将图像路径存储在数据库中的用户表中
回答by M.Elwan
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 App\User;
您尚未$user
在控制器中初始化变量,因此在使用它之前,您必须添加$user = new User;
假设您已经拥有use App\User;
or $user = new \App\User;
if you don't.
或者$user = new \App\User;
如果你不这样做。
Updateif 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
通过根据应在请求中发送的主键进行选择来初始化变量,然后进行如下初始化:
$user_id = $request->input('user_id');
$user = User::find($user_id);
回答by Jose Mhlanga
Use the simple way below:
使用下面的简单方法:
$user = User::where('id', '=', $id)->first(); // where id is method param from url or request object
$user->save();
This is also easier when updating multiple tables where the ids in the sub-tables have foreign keys and need to be explicitly defined in the update process.
这在更新多个表时也更容易,其中子表中的 id 具有外键并且需要在更新过程中明确定义。
Also $user = User::find($id);
normally work where the $id
is the primary key of that object table
也$user = User::find($id);
通常在$id
是该对象表的主键的地方工作