How do I handle a not found case when using Laravel Dependency Injection?

Viewed 893

I am using a route /news/{news} and the controller public function show(News $news). When I supply the route with a valid ID, it all works fine and dandy. However, when the ID isn't found, Laravel returns a 404-page. I would rather handle that case myself, since I am using it as an API.

My question resembles this one, but I want to use Dependency Injection, if at all possible. I know I can do a normal News::where(), but that is not how I'd like to solve this. The question, therefor, is; How do I handle a not found case when using Laravel Dependency Injection?

I am building a private API so what I'd like to achieve is to return a JSON value when the news isn't found.

2 Answers
Related