drpogkqqi536984960 2017-05-08 10:35
浏览 62
已采纳

Laravel更新方法错误

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

  • 写回答

2条回答 默认 最新

  • douzen1896 2017-05-08 10:49
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度