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 Use WYSIHTML5 Editor In Laravel 9?


wysihtml5 editor example, laravel wysihtml5 editor example, wysihtml5 laravel, wysihtml5 editor example in laravel, wysihtml5 with laravel, wysihtml5...

Laravel 9 Form Validation Example


In this Laravel 9 validation tutorial, I'll demonstrate how to validate form input and provide an error message before saving it to the database.

How to install Adminer in Laravel application


In this article we will install Adminer in Laravel 8 application

Laravel whereDate and whereDay Example


laravel whereDate and whereDay example, whereDate, whereDay, where condition in laravel, laravel date format, wheredate and whereDay in laravel 8

Laravel 8 Create Custom Helper Functions


Laravel 8 Create Custom Helper Functions In this tutorial we will see how to Create Custom Helper Functions in Laravel 8.