Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Tutorial Free

Laravel API: Override 404 Error Message in Route Model Binding

January 16, 2023
2 min read

If you use Route Model Binding in your API Controllers and the record is not found, it will automatically return the 404 status code with an error message like "No query results for model [App\Models\User] 1". How can you override it?

I'm talking about this Controller example:

VehicleController.php:

public function show(Vehicle $vehicle) {
// return the API response with data
}

In case of the model is not found in the routes/api.php based routes, it returns the JSON like this:

{
"message": "No query results for model [App\\Models\\Vehicle] 1",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"file": "/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
"line": 391,
"trace": [
// ... full stack trace of the error
]
}

But maybe you want to have another error than "No query results for model [App\Models\Vehicle] 1"?

One reason for this may be just security: this message exposes your back-end Model name and just tells the API consumer that we probably have a Laravel project. So, any information we may hide from the consumer, increases our security, in theory.

To override this message, you need to override the whole NotFoundHttpException and return your own structure.

For that, we go to the Laravel Exception Handler file that comes by default with Laravel, and add this to the register() method:

app/Exceptions/Handler.php:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
class Handler extends ExceptionHandler
{
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
if ($request->is('api/v1/vehicles/*')) { // <- Add your condition here
return response()->json([
'message' => 'Vehicle record not found.'
], 404);
}
});
}
}

Now if you have a 404 on Vehicles, it will just return the JSON:

{
"message": "Record not found."
}

As you can see, we have an if-statement for the condition on when we want to override the message. It may be a specific endpoint for a specific model, or you may want to do it for the whole api/* with some generic message.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

GK
Gavin Kimpson ✓ Link copied!

exactly what I was looking for thanks 🤠

G
gmrk08 ✓ Link copied!

great... thanks for that :)

A
augusto-dmh ✓ Link copied!

to get the same result easier is just necessary to modify APP_DEBUG to false in .env.

and in Laravel 11 the syntax is different

Thanks for the post!

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.