0

i have a method to update image path to data base but i'm getting this error

ErrorException in DriversController.php line 296:
Attempt to assign property of non-object

in my controller

public function updateDriver(Request $request, $id)
{
    $driver = Driver::find($id)->update($request->all());

    if($request->hasFile('profile-photo')) {

            $image_file = $request->file('profile-photo');

            $get_image_name = $request['first_name'].$request['phone_number'].'.'.$image_file->getClientOriginalExtension();
            $image_name = preg_replace('/\s+/', '', $get_image_name);

            $s3 = \Storage::disk('s3');
            $filePath = '/drivers/' . $image_name;
            $s3->put($filePath, file_get_contents($image_file), 'public');

            $driver->profile_photo = $image_name;   //this is ware the error line 296 
            $driver->save();
    }

    return redirect()->back()->with('message', 'Driver updater successfully');
}

Thank you

7
  • And line 296 is where in this 22 lines of code? Commented May 8, 2017 at 10:37
  • @RiggsFolly thanks for your time, the line is $driver->profile_photo = $image_name; Commented May 8, 2017 at 10:40
  • can you post the output of print_r($driver)? Commented May 8, 2017 at 10:43
  • $driver = Driver::find($id)->update($request->all()); this line failed.. you $driver variable is null or false.. that's why you can't assign anything to it.. Commented May 8, 2017 at 10:43
  • 1
    $driver->profile_photo? how will you get it? as there is no object thus that error. use something like this by removing these two lines $driver->profile_photo = $image_name; $driver->save(); to this $driver = Driver::find($id)->update(array('profile_photo'=>$image_name)); Commented May 8, 2017 at 10:54

2 Answers 2

2

$driver will be a Boolean (I believe) after the update.

Try:

$driver = Driver::find($id);
$driver->update($request->all());
Sign up to request clarification or add additional context in comments.

Comments

1

You can update your code like :

public function updateDriver(Request $request, $id)
{
    $driver = Driver::find($id)->update($request->all());

   if($request->hasFile('profile-photo')) {

           $image_file = $request->file('profile-photo');

           $get_image_name = $request['first_name'].$request['phone_number'].'.'.$image_file->getClientOriginalExtension();
            $image_name = preg_replace('/\s+/', '', $get_image_name);

           $s3 = \Storage::disk('s3');
            $filePath = '/drivers/' . $image_name;
            $s3->put($filePath, file_get_contents($image_file), 'public');

           $driver = Driver::find($id)->update(array('profile_photo'=>$image_name));

   }

   return redirect()->back()->with('message', 'Driver updater successfully');
}

1 Comment

thank you for your time really appreciate it, it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.