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

Install and Use Font Awesome Icons in Laravel 9


laravel 9 install font awesome icons example,how to install font awesome icons in laravel 9,install font awesome icons example,how to install font awe...

How to Create REST API in Laravel7 using Passport


rest api in laravel 7/6 step by step, rest api in laravel 7/6 tutorial, laravel 7/6 restful api authentication, laravel 7/6 passport rest api tutorial

Laravel 9 Add Watermark on Image


In this article, we'll show you how to use the Laravel application to apply a text overlay watermark on an image.

Create Zip file and Download in Laravel 9


After we have compressed the files, let's download the Laravel 9 zip files. In this tutorial, we'll use the ZipArchive class and a Laravel package to...

How To Create Word Document File In Laravel


In Laravel, use the phpoffice/phpword package to create a word document file. In this example, I&amp;#039;ll show you how to create a word document an...