我想在数据库中存储图像路径.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 - 错误:从空值创建默认对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!