Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

php - laravel-5.4 - error :Creating default object from empty value

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());
}

I am getting the following error: ErrorException in RegistersUsers.php line 40: Creating default object from empty value enter image description here.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

or $user = new AppUser; if you don't.

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...