【问题标题】:Laravel Update Method ErrorLaravel 更新方法错误
【发布时间】: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 变量是nullfalse.. 这就是为什么你不能给它分配任何东西..
  • $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


【解决方案1】:

$driver 在更新后将是一个布尔值(我相信)。

试试:

$driver = Driver::find($id);
$driver->update($request->all());

【讨论】:

    【解决方案2】:

    你可以像这样更新你的代码:

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

    【讨论】:

    • 感谢您的宝贵时间,非常感谢,它成功了
    猜你喜欢
    • 2013-05-28
    • 2017-09-14
    • 2018-06-12
    • 2016-01-15
    • 1970-01-01
    • 2016-04-30
    • 2016-01-10
    • 2015-08-03
    • 1970-01-01
    相关资源
    最近更新 更多