Laravel 8 Custom 404, 500 Error Page Example. In this tutorial, we will understand how to create custom error page in Laravel 8 and we will also try to tell you why we required to create the custom error page.
Before going further we also need to check the default error page in Laravel framework. Laravel has the default error page (404,500) with default design but on our website your gets such type of errors at that time most of the chances are the user will get bounced and will never search for our website, So for that we need to create our own custom error page where we can try to create the good design so that user will stay on our website.

So, Lets start with the tutorial and will see step by step from scratch for custom error page.
First we need to understand that for which error we need to create the page like 404, 500 etc. so accordingly you have to create the separate blade file for each error like 404.blade.php, 500.blade.php etc.
You need to follow the following steps.

Table of Content

  1. Create 404 View File 
  2. Create 500 View File 
  3. Modify Exceptions Handler 

1. Create 404 View File

You can create the blade file anywhere in views folder but in this example we will create the separate folder for error file.
So here we will create the errors folder inside the resources/views, inside this folder create 404.blade.php the blade file.

<!DOCTYPE html>
<html>
<head>
    <title>Page Not Found</title>
</head>
<body>
This is the custom 404 error page.
</body>
</html>

2. Create 500 View File

Now, we already having the resources/views/errors folder, so additionality we don?t need to create other folder just create the 500.blade.php.

<!DOCTYPE html>
<html>
<head>
    <title>Page Not Found</title>
</head>
<body>
This is the custom 500 error page.
</body>
</html>

3. Modify Exceptions Handler

Now, go-to app/Exceptions and open Handler.php file and you need to find the render() method. Then do some changes in the render() method as shown bellow:

public function render($request, Exception $exception)
{
    if ($this->isHttpException($exception)) {
        if ($exception->getStatusCode() == 404) {
            return response()->view('errors.' . '404', [], 404);
        }
    }
    return parent::render($request, $exception);
}

In the above example we have render only 404 error page, now we will do the same process for 500 error page.

public function render($request, Exception $exception)
{
    if ($this->isHttpException($exception)) {
        if ($exception->getStatusCode() == 404) {
            return response()->view('errors.' . '404', [], 404);
        }
        if ($exception->getStatusCode() == 500) {
            return response()->view('errors.' . '500', [], 500);
        }
    }
    return parent::render($request, $exception);
}

I hope you will like the content and it will help you to learn Laravel 8 Custom 404, 500 Error Page Example
If you like this content, do share.


Recommended Posts

View All

How to Install and Use MomentJS in Laravel 9 Application


Learn how to easily install and use MomentJS in your Laravel 9 application with our step-by-step guide. Improve your date and time management today!

Laravel 9 Livewire Form Submit Tutorial


Laravel 9 Work With Livewire Form Submit Tutorial. laravel 9 livewire form submit example, create form with laravel 9 livewire, laravel 9 livewire tut...

Laravel 9 Socialite Login with LinkedIn Tutorial Example


How to use the Laravel socialite, Livewire, and Jetstream libraries to create a LinkedIn login system from scratch in Laravel

Laravel whereHas and orWhereHas Query Example


laravel whereHas and orWhereHas query example, wherehas and orWhereHas query in laravel, how to use wherehas in laravel

Laravel 8 Stripe Subscription Tutorial Using Cashier Example


laravel cashier, stripe laravel, laravel subscription tutorial, laravel 8 cashier tutorial, laravel 8 stripe subscription tutorial