I got this error when I tried to add a feature of delete, to the posts just by clicking on the delete button. Where am I doing wrong?
Delete post function in PostController:
public function getDeletePost($post_id)
{
$post =Post::where('id',$post_id)->first();
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
$post
object is null. Maybe you are sending a wrong $post_id. If you check that the post exists before delete you avoid that error.
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!!']);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With