Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

IvanPujianto's avatar

How to custom error msg for NotFoundHttpException ?

im using laravel 11, i try to catch exception and then display the custom localized error msg, but its not working for NotFoundHttpException, the session for error msg is not exists. So, how can i custom error msg for NotFoundHttpException ?

This is my code :

When i try to test ModelNotFoundException, its working, i can see the error msg on the browser, but its not working for NotFoundHttpException, how to make it work for NotFoundHttpException too ?

and then when i try to dd(session()->all()), i can see the 'error' property on session object for ModelNotFoundException, but its not the case with NotFoundHttpException

Thank u.

0 likes
5 replies
LaryAI's avatar
Level 58

To customize the error message for a NotFoundHttpException in Laravel, you need to ensure that the exception is being caught and handled properly. The issue you're facing is likely due to the fact that a NotFoundHttpException is typically triggered when a route is not found, and Laravel's default behavior is to render a 404 page directly, bypassing any redirection logic you might have in your exception handler.

Here's a solution to customize the error message for a NotFoundHttpException:

  1. Override the render method in your Handler class: Make sure your custom exception handler is correctly set up to handle the NotFoundHttpException.

  2. Return a custom view or response: Instead of redirecting back, you can return a custom view or response directly from the render method.

Here's an example of how you can modify your Handler class:

  1. Create a custom error view: Create a view file at resources/views/errors/custom.blade.php to display your custom error message.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error</title>
</head>
<body>
    <h1>{{ $message }}</h1>
</body>
</html>

By returning a custom view, you ensure that the user sees your custom error message when a NotFoundHttpException occurs. This approach avoids the issue of session data not being available due to the nature of how 404 errors are handled in Laravel.

1 like
martinbean's avatar

@ivanpujianto Why on earth are you trying to change the ModelNotFoundException status code from the appropriate 404, to the inappropriate 500?

1 like
IvanPujianto's avatar

@martinbean Thank u for ur reply, its just an experiment for learning laravel, so i ignore the code for now ... but thank u for ur suggestion.

tisuchi's avatar

@ivanpujianto Your ModelNotFoundException status code 500 is misleading!

5xx: some errors in your server. https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/500

For your case, you can use either 404 as @martinbean suggested, otherwise can you 405

The 404 is the right stats code for the [ModelNotFoundException which is used by Laravel as well: https://laravel.com/api/master/Illuminate/Database/Eloquent/ModelNotFoundException.html

1 like
IvanPujianto's avatar

@tisuchi Thank u for ur reply, its just an experiment for learning laravel, so i ignore the code for now ... but thank u for ur suggestion. appreciate it ...

1 like

Please or to participate in this conversation.