尝试通过单击删除按钮将删除功能添加到帖子时,出现此错误。我在哪里做错了?
删除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
对象为null。也许您发送的是错误的$ 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!!']);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句