larvel 9编辑/更新功能返回呼叫到null上的成员函数商店()
我有一个博客,我在其中创建一个更新功能,然后从“图像”字段中删除了所需的参数并将其设置为“无效”,因为为用户提供了一个选择图像是否愿意的选择是有意义的创建他们的帖子后。
更具体地更新功能
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您拥有新的映像文件时,才应调用
store
方法:然后仅在拥有新image时才添加
$ image_path
属性:You should call the
store
method only when you have new image file:and then add
$image_path
to attributes only when you have new image: