Laravel更新方法错误

时间:2017-05-08 10:35:18

标签: php sql laravel eloquent laravel-5.4

我有一种方法来更新数据库的图像路径,但我收到此错误

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

在我的控制器中

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

谢谢

1 个答案:

答案 0 :(得分:1)

您可以更新代码,如:

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