How to create a custom error page with variables in laravel 8

Viewed 493

Good morning everyone, how to create a custom error page with variables in laravel 8 ? So, I want to display an error page with @extend('layouts.app') where in this layouts.app I give a variable such as $general to display the data in the generals table.

I've tried with the code below, but the result is still Undefined variable: general.

Exceptions\Handler.php

<?php

namespace App\Exceptions;

use App\Models\General;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Auth\AuthenticationException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        $guard = Arr::get($exception->guards(), 0);

        $route = 'login';

        if ($guard == 'admin') {
            $route = 'admin.login';
        }

        return redirect()->route($route);
    }

    public function render($request, Throwable $exception)
    {
        
        if($this->isHttpException($exception)){
            switch ($exception->getCode()) {
                case 404:
                    //return redirect()->route('404');
                    $general = General::find(1);
                    return response()->view('errors.404', ['general'   => $general], $exception->getCode());
                    break;
                case 405:
                    return response()->view('errors.405', [], $exception->getCode());
                    break;
                case 500:
                    return response()->view('errors.500', [], $exception->getCode());
                    break;
            }
        }        
        return parent::render($request, $exception);
    }
}

errors\404.blade.php

@extends('layouts.front')

@section('title', __('Not Found'))
@section('code', '404')
@section('message', __('Not Found'))

Undefined variable: general

what is the correct way to add variables in laravel 8 error page? thanks :)

1 Answers

You can create your own service provider (or just use App\Providers\AppServiceProvider)

In boot method register View::composer, which will listen on error-page views

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        // Using closure based composers...
        View::composer('errors::404', function ($view) {
            $general = General::find(1);
            $view->with('general', $general);
        });
    }

you can also use wildcard 'errors::*'

Related