php - 404 page not working for some routes in laravel 5.4 -


i have route this:

web.php

route::get('post/{slug}', [     'as'   => 'post.single',     'uses' => 'pagecontroller@getsingle', ]); 

pagecontroller.php

public function getsingle($slug) {     //some db stuff , returning array view     return view('single', array('var1' => $var, 'var2' => $var2)); } 

a post has slug stored in database. if post exists slug, example: first-post route mysite.com/post/first-post works expected. if post doesn't exists, example: second-post route mysite.com/post/second-post gives me error:

**errorexception** trying property of non-object 

i want show 404 error page (404 page configured) mysite.com/hellohello gives , 404 page, it's working expected.

any suggestion should do?

eloquent has neat firstorfail method either returns first entity finds, or if there none throws modelnotfoundexception laravel converts 404 page.

public function getsingle($slug) {     $var = yourmodel::where('slug', $slug)->firstorfail();     return view('single', compact('var')); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -