Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to store image path in database. My Controller code undervendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.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

like image 355
dynamic Avatar asked Apr 19 '17 07:04

dynamic


People also ask

What is creating default object from empty value in Laravel?

Laravel Creating default object from empty value. And apparently the error specified Creating default object from an empty value it is because the object hasnt been instantiated or hasnt been created. Creating default object from empty value Heres the code Im using.

What is errorexception in Laravel?

The error given is an impact from executing a script of web-based application powered by Laravel framework. The following explanation will use a certain file and certain snippet code for an example : The error as stated as an ErrorException is located as follows :

Why is my foreach loop not working in Laravel?

You see, the foreach loop statement only works with arrays and objects (that are iterable). If you use the foreach loop statement with other data types, you will get an error, and that’s the exact error you get in any version of Laravel. How do I solve this Laravel error? Originally Answered: How do I solve this laravel error?

What is an uncaught error in Laravel?

An ‘Uncaught Error’ is a fault generated by the operating system, and NOT being handled correctly. ‘$injector: usually means something is getting attached, and the fault is in a module. not happen? What has been changed since?” This is the most probable search path, in my opinion. Who created Laravel? Laravel.


1 Answers

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;

or $user = new \App\User; 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);
like image 172
M.Elwan Avatar answered Oct 14 '22 08:10

M.Elwan