Umair Mehmood
Umair Mehmood

Reputation: 562

NotFoundHttpException No query results for model [App\Post] laravel 5.4

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

http://laravel5.prc/posts/4

Can any body guide me. thanks in advance

Upvotes: 0

Views: 443

Answers (1)

ajthinking
ajthinking

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

Related Questions