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

thebigk's avatar
Level 13

Customizing the message in Laravel's new error pages?

Apparently, this is straightforward, but I'm unable to find out how. There are new error pages in Laravel 5.7; which I like. I only want to be able to customize the 'text' shown on the error page. Is that possible? I've tried the obvious ones -

  1. abort(404, "My Error Message")

  2. Created custom 404.blade.php inside errors but it simply replaces the default error page with mine.

Would appreciate help. Thank you in advance.

0 likes
8 replies
vajid's avatar

In 5.7 we can publish error pages with vendor:publish

This worked for me in my case when I had to change the error pages.

thebigk's avatar
Level 13

@vajid - I only want to change the message when I abort or otherwise. I there a way I could simply pass the custom message when aborting while using the graphics of the current pages?

vajid's avatar

I dont know, in this my work around will be something like this

//just before aborting with
abort(404);
 //you can set your message in session
session()->put( "error_404","My Error Message");

// 404.blade.php

@section('message', __(session()->pull('error_404','Sorry, the page you are looking for could not be found..')))
sustained's avatar

The abort helper raises an exception which will then be caught by the exception handler, which will decide which view to render. As such, aborting and the rendering of error pages is tied to the HTTP status code.

You can see what the views look like by navigating to the vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/ directory, they are rather simple.

You are free to override them on a status code-by-status code basis by creating a file in your resources/views/errors directory as you have found out.

You are also free to extend the built-in layout when you do so, if that's how you want them to look:

@extends('errors::illustrated-layout')

Some of the error pages, like the 403 page, do actually allow you to display a message by default because of this line of code in the view:

@section(
    'message',
    __($exception->getMessage() ?: 'Sorry, you are forbidden from accessing this page.')
)

As you can see, if there is a message it will be displayed.

Try it yourself with abort(403, 'my message');.

Others like the 404 page instead have something like this:

@section('message', __('Sorry, the page you are looking for could not be found.'))

You are free to change any template to use $exception->getMessage(), just like the 403 error page does, then the message passed to abort will be displayed.

Vilfago's avatar

And __() is an helper for localization, so you can also add a message in lang files, if it's a matter of translation

thebigk's avatar
Level 13

@sustained - Thank you for the answer. I tried customizing the error page by extending the message; but it throws errors.

I even tried copying the entire 404 from vendor in my errors/404.blade.php and now I'm getting following error-

No hint path defined for [errors]. (View: <mylocation>/resources/views/errors/404.blade.php)

sustained's avatar

@THEBIGK - I was getting the same error at one point when my view was NOT in /resources/views/errors/ but when I moved it into that exact directory that error went away.

I never did find out what exactly was going on.

Laravel version? 5.7 or lower?

You can always as a temporary stopgap copy the layout file into the errors directory and use errors.layout instead, see here.

Please or to participate in this conversation.