How to Create Custom 404 Page in Laravel 8 Application

Last updated on by Digamber

Through this Laravel tutorial, we would like to share with you how to easily create a custom error page 404 and 403, 500, 419, 255, 405 error pages in the Laravel 8 application.

While working on a web application errors or more precisely exception can manifest at any time, Laravel framework handles exceptions beautifully; It offers a handy error handler class which looks for almost every errors displayed in Laravel environment and returns an adequate response.

Generically, you can get the default error response if you configure debug property to false; nevertheless, you can create a custom error handling template.

Let’s handle custom errors in Laravel 8.

Create New Laravel Project

Start this tutorial by creating a brand new Laravel project using the following command:

composer create-project laravel/laravel --prefer-dist laravel-error-handling-example

Create Custom 404 Error Page

You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404.blade.php file. It will redirect you to the 404 page if you don’t find the associated URL.

Similarly you can create rest of the error handling blade views for 403, 500, 419, 255 and 405 exceptions:

Include the following code in resources/views/errors/404.blade.php error file.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>404 Custom Error Page Example</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

</head>

<body>
    <div class="container mt-5 pt-5">
        <div class="alert alert-danger text-center">
            <h2 class="display-3">404</h2>
            <p class="display-5">Oops! Something is wrong.</p>
        </div>
    </div>
</body>

</html>

To test out the 404 custom error template, you need to start the application.

php artisan serve

As you know, 404 errors occur when you visit the non-existed link, so type the wrong URL in the browser’s address bar.

http://127.0.0.1:8000/not

Create Custom 404 Page in Laravel 8

Easy, isn’t it. This tutorial is over, i hope you liked it.