Reputation: 562
i am writing this code in my postController for checking the post exists or not. but the first part of if conduction is working but the else is not working, its showing me
(2/2) NotFoundHttpException No query results for model [App\Post].
$post = Post::findOrFail($post->id);
if(count($post)>0)
{
return view('posts.show',compact('post'));
}
else{
return view('404error');
}
the purpose of this code is, if post found the Good else redirect it to 404 page. Url of my page is some thing like this. i am just learning the laravel, and on beginner level
Can any body guide me. thanks in advance
Upvotes: 0
Views: 443
Reputation: 4558
You might want to remove typehinting (Post $post) and replace it to this:
public function show($id)
{
//
$post = Post::find($id);
if($post)
{
return view('posts.show',compact('post'));
}
else{
return view('404error');
}
}
Upvotes: 2