我来晚了,但我遇到了这个问题,解决方案完全不同。
在我的 PostController 里,我检查了 auth,然后找到:
Post::where('slug', $oldSlug)->first()->update([
'image_url' => $request->image_url,
'slug' => $newSlug,
'title' => $request->title,
'body' => $request->body,
'description' => $request->description,
'user_id' => auth()->user()->id,
'catagory' => $request->catagory,
]);
我在查看它,试图理解为什么它不喜欢“image_url”,大声喊叫“添加['image_url']到fillable属性中以允许批量赋值”。
我意识到我添加了->first()
,以确保我只获取一篇文章,但由于所有的slug都经过验证和确认是唯一的,所以我删除了->first()
。
Post::where('slug', $oldSlug)->update([
'image_url' => $request->image_url,
'slug' => $newSlug,
'title' => $request->title,
'body' => $request->body,
'description' => $request->description,
'user_id' => auth()->user()->id,
'catagory' => $request->catagory,
]);
就像魔术一样,它可以工作。
如果有更高级的人能解释为什么它能工作,我会很感兴趣。谢谢!