【问题标题】:Call to a member function store() on null Laravel 8在 null Laravel 8 上调用成员函数 store()
【发布时间】:2021-11-24 07:03:35
【问题描述】:

当我点击保存以更新我的编辑页面上的数据时,我收到以下错误。

在 null 上调用成员函数 store()

public function update(User $user)
{
    $this->authorize('update', $user->profile);
    
    $data = request()->validate([
        'title' => 'required',
        'description' => 'required',
        'url' => 'url',
        'image' => '',
    ]);
    
    $imagePath = request('image')
        ->store('profile', 'public');
    $image = Image::make(public_path("storage/{$imagePath}"))
        ->fit(1000, 1000);
    $image->save();
    
    dd($data);
    
    auth()->user->profile->update(array_merge(
        ['image' => $imagePath]
    ));
    
    return redirect("/profile/{$user->id}");
}

【问题讨论】:

  • request('image')null。有什么不清楚的吗?你是如何上传这张图片的?您输入的name="image" 是否正确?您是否将enctype="multipart/form-data" 添加到您的<form> 元素中?这是一个 AJAX 请求吗?请尝试提供更多信息。相关代码的错误很好,但是您在这里缺少大约一半的难题(此图像上传的整个前端逻辑)。 Edit your question 包括那个,否则没有人可以帮助你而不用猜测。
  • 除了上面的评论,我还以为是request()->file('image')->store..(但request('image')如果不是空的话也可以用)

标签: php laravel


【解决方案1】:

由于 $request->file('file') 返回 null,而你试图在 null 上调用方法,导致异常。

由于 Laravel 处理 PUT 和 PATCH 请求的方式,您需要在 POST 请求中发送您的请求,并在标头中提供带有值 PUT 的 _method。这是 Laravel 所期望的。

'image' => 'required'

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2020-10-28
    • 2021-04-25
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多