Skip to content
Advertisement

Custom findOrFail for the API routes

I think this should be implemented by default since I’m working in routes/api.php.

I want to give a 404 error JSON response in case that we don’t find any rows for the given id argument on findOrFail() method.

Something like:

JavaScript

Instead of the default Sorry, the page you are looking for could not be found. blade page.

I don’t want to do:

JavaScript

Everywhere when I getting an id, and I wouldn’t like to implement this in a middleware since it will cause some ‘mess’ on the api.php file.

Advertisement

Answer

You can always catch the exception in the AppExceptionsHandler.php

Import the exception into the class using the following:

JavaScript

and in the render method, add

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement