当我在Laravel8“从空值创建默认对象”的博客项目中更新帖子时,我看到了这个错误,这是什么意思?

lkaoscv7  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(306)

当我试图在博客项目中编辑我的文章时,出现了上述错误。这是我的代码:

  1. public function update(Request $request)
  2. {
  3. //
  4. $data = Post::find($request->id);
  5. $data->title = $request->input('title');
  6. $data->description = $request->input('description');
  7. $data->update();
  8. return redirect('/index')->withMessage('successfully published');
  9. }

这就是我的路线-
路由::get('posts/edit/{edit}',[postcontroller::class,'edit']);
路由::post('/update',[postcontroller::class,'update']);

bd1hkmkf

bd1hkmkf1#

post::find($id)返回null-您正试图分配给null上的属性。
您应该改为使用findorfail($id),否则,请检查是否已返回post示例,以便在开始分配之前排除方法错误。

相关问题