11

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

dynamic
  • 197
  • 1
  • 3
  • 13

2 Answers2

11

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);
M.Elwan
  • 1,904
  • 1
  • 16
  • 21
  • he's using it as a laravel user object so that he can easily use ` $user->save();` – M.Elwan Apr 19 '17 at 07:31
  • I have `use App\User;` n code already. I dont have to do `$user =new User;` n code because this public function register-> Handle a registration request for the application. means i already got user name and image registered and doing validation.`$this->validator($request->all())->validate();`. if I do $user =new User , then i will be inserting new value to the user. in the database i have an entry for image which is now "C:\xampp\tmp\php469E.tmp". My need is to update the tmp path to public folder path. – dynamic Apr 19 '17 at 07:45
  • inside `register` function's scope there's no variable `$user` so it's not defined. Are trying to update a user record ? – M.Elwan Apr 19 '17 at 07:49
  • Yes.Updating the image in user record in database.If you check the error image which i uploaded-> I am updating the image in user table in database with image path instead of tmp path – dynamic Apr 19 '17 at 08:04
2

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.

Also $user = User::find($id); normally work where the $id is the primary key of that object table

Jose Mhlanga
  • 805
  • 12
  • 14