当我尝试通过单击删除按钮向帖子添加删除功能时出现此错误。
我在哪里做错了?
删除 PostController 中的 post 函数:
public function getDeletePost($post_id)
{
$post =Post::where('id',$post_id)->first();
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
最佳答案
$post
对象为空。也许您发送了错误的 $post_id。如果在删除之前检查该帖子是否存在,则可以避免该错误。
public function getDeletePost($post_id)
{
$post =Post::where('id',$post_id)->first();
if ($post != null) {
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
return redirect()->route('dashboard')->with(['message'=> 'Wrong ID!!']);
}
关于laravel:在 null 上调用成员函数 delete(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40421415/