Scenario: A post has a delete button in all posts list view and the postShow view. So if the post is deleted from the list it will return back()->with(‘success’, ‘Some message’) but if we delete it from the postShow view then redirect to the home.
(in short if the the post is deleted via show page then it redirects to home else redirect back)
{
$post = Post::findOrFail($id);
$post->delete();
if(route('post.show', $post->slug)){
return redirect()->route('home')->with(["message" => "Your Post has been deleted successfully");
} else {
return back()->with(["message" => "Your Post has been deleted successfully");
}
}
This is the code but it is always redirecting to home.