問題描述
我想在數據庫中存儲圖像路徑.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模板網!