【发布时间】:2017-05-08 10:35:18
【问题描述】:
我有一种方法可以将图像路径更新到数据库,但出现此错误
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');
}
谢谢
【问题讨论】:
-
而第 296 行在这 22 行代码中的什么位置?
-
@RiggsFolly 感谢您抽出宝贵时间,电话是
$driver->profile_photo = $image_name; -
你能发布 print_r($driver) 的输出吗?
-
$driver = Driver::find($id)->update($request->all());这一行失败.. 你$driver变量是null或false.. 这就是为什么你不能给它分配任何东西.. -
$driver->profile_photo?你将如何得到它?因为没有对象,因此该错误。通过删除这两行 $driver->profile_photo = $image_name; 来使用类似的东西$驱动程序->保存();到这个 $driver = Driver::find($id)->update(array('profile_photo'=>$image_name));
标签: php sql laravel eloquent laravel-5.4