【问题标题】:Call to a member function move() on null when update in laravel在 laravel 中更新时在 null 上调用成员函数 move()
【发布时间】:2021-01-08 14:21:58
【问题描述】:

我在尝试更新数据时遇到此错误。如果我更新图像没有错误,但如果我没有显示调用成员函数 move() on null

这是我的代码:

public function update($id, Request $request)
{   
    $change = Profile::findorfail($id);
    $before = $change->foto_profil;
        
    $profile = [
        'fullname' => $request['fullname'],
        'phone' => $request['phone'],
        'ttl' => $request['ttl'],
        'foto_profil' => $before
    ];
    $request->foto_profil->move(public_path().'/image', $before);
    $change->update($profile);

    return redirect('/profil');
}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您可以使用hasFile() 方法确定请求中是否存在文件:

    if($request->hasFile('foto_profil')){
         $request->foto_profil->move(public_path().'/image', $before);
    }
    

    查看文档here

    【讨论】:

      【解决方案2】:

      如果请求中存在照片,只需添加验证

      if($request->foto_profil) {
         $request->foto_profil->move(public_path().'/image', $before);
      }
      

      【讨论】:

        猜你喜欢
        • 2022-10-16
        • 2019-11-22
        • 2020-06-05
        • 1970-01-01
        • 2021-01-03
        • 2021-11-25
        • 1970-01-01
        • 2016-09-04
        • 2016-06-29
        相关资源
        最近更新 更多