larvel 9编辑/更新功能返回呼叫到null上的成员函数商店()

发布于 2025-02-11 16:01:16 字数 1297 浏览 0 评论 0原文

我有一个博客,我在其中创建一个更新功能,然后从“图像”字段中删除了所需的参数并将其设置为“无效”,因为为用户提供了一个选择图像是否愿意的选择是有意义的创建他们的帖子后。

更具体地更新功能

public function update(Request $request, Posts $post) {
    $formFields = $request->validate([
        'title' => 'required|min:3|max:50',
        'image' => 'nullable|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
        'sub_title' => 'required|min:3|max:100',
        'tags' => 'required|min:3|max:20',
        'content' => 'required',
        'featured' => 'nullable',
    ]);

    $image_path = $request->file('image')->store('thumbnails', 'public');

    if($request->has('featured')) {
        Posts::where('featured', '=',1)
        ->update(['featured' => false]);
    }

    $post->update([
        'title' => $request->post('title'),
        'image' => $image_path,
        'sub_title' => $request->post('sub_title'),
        'tags' => $request->post('tags'),
        'content' => $request->post('content'),
        'featured' => ($request->has('featured')) ? true : false,
    ]);

    return redirect('/')->with('message', 'Post updated successfully!');  
}

,在此行上指出了该错误:

$image_path = $request->file('image')->store('thumbnails', 'public');

I have a blog where I am creating an update function and I removed the required parameter from my 'image' field and set it to 'nullable' since it makes sense to give the user an option to change the image if they want to or not after creating their post.

Update function

public function update(Request $request, Posts $post) {
    $formFields = $request->validate([
        'title' => 'required|min:3|max:50',
        'image' => 'nullable|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
        'sub_title' => 'required|min:3|max:100',
        'tags' => 'required|min:3|max:20',
        'content' => 'required',
        'featured' => 'nullable',
    ]);

    $image_path = $request->file('image')->store('thumbnails', 'public');

    if($request->has('featured')) {
        Posts::where('featured', '=',1)
        ->update(['featured' => false]);
    }

    $post->update([
        'title' => $request->post('title'),
        'image' => $image_path,
        'sub_title' => $request->post('sub_title'),
        'tags' => $request->post('tags'),
        'content' => $request->post('content'),
        'featured' => ($request->has('featured')) ? true : false,
    ]);

    return redirect('/')->with('message', 'Post updated successfully!');  
}

More specifically, the error is being pointed out on this line:

$image_path = $request->file('image')->store('thumbnails', 'public');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2025-02-18 16:01:16

仅当您拥有新的映像文件时,才应调用store方法:

$image_path = $request->file('image')?->store('thumbnails', 'public');

然后仅在拥有新image时才添加$ image_path属性:

$attributes = [
        'title' => $request->post('title'),
        'sub_title' => $request->post('sub_title'),
        'tags' => $request->post('tags'),
        'content' => $request->post('content'),
        'featured' => ($request->has('featured')) ? true : false,
    ];

if ($image_path) {
    $attributes['image'] = $image_path;
}

$post->update($attributes);

You should call the store method only when you have new image file:

$image_path = $request->file('image')?->store('thumbnails', 'public');

and then add $image_path to attributes only when you have new image:

$attributes = [
        'title' => $request->post('title'),
        'sub_title' => $request->post('sub_title'),
        'tags' => $request->post('tags'),
        'content' => $request->post('content'),
        'featured' => ($request->has('featured')) ? true : false,
    ];

if ($image_path) {
    $attributes['image'] = $image_path;
}

$post->update($attributes);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文