What you want is documented here: http://laravel.com/docs/5.0/errors#handling-errors
Feb 26, 2015
15
Level 6
Where to handle 404 for Route Model Binding when model not found
I have route-model binding set up:
// bind news using slug
$router->bind('newsslug', function($slug) {
return NewsPost::slug($slug)->first();
});
but if the slug is bad, I'll get a null value for my model, which means I want ot give the user a 404 - bad url. If I use firstOrFail(), I get:
No query results for model [App\NewsPost].
so what's the best way to handle a 404 in this case - in the model binding:
$post = NewsPost::slug($slug)->get();
if ($post->isEmpty()) abort(404);
return $post->first();
or in the controller, or in an another way? Thanks...
Please or to participate in this conversation.