Add custom 500 error page only for production in Laravel

Simply add this code in \App\Exceptinons\Handler.php:

public function render($request, Exception $exception)
{
    // Render well-known exceptions here

    // Otherwise display internal error message
    if(!env('APP_DEBUG', false)){
        return view('errors.500');
    } else {
        return parent::render($request, $exception);
    }
}

Or

public function render($request, Exception $exception)
{

    // Render well-known exceptions here

    // Otherwise display internal error message
    if(app()->environment() === 'production') {
        return view('errors.500');
    } else {
        return parent::render($request, $exception);
    }
}

I found the best way to solve my problem is to add the following function to App\Exceptions\Handler.php

protected function renderHttpException(HttpException $e)
{
    if ($e->getStatusCode() === 500 && env('APP_DEBUG') === true) {
        // Display Laravel's default error message with appropriate error information
        return $this->convertExceptionToResponse($e);
    }
    return parent::renderHttpException($e); // Continue as normal 
}

Better solutions are welcome!


Add the code to the app/Exceptions/Handler.php file inside the Handler class:

protected function convertExceptionToResponse(Exception $e)
{
    if (config('app.debug')) {
        return parent::convertExceptionToResponse($e);
    }

    return response()->view('errors.500', [
        'exception' => $e
    ], 500);
}

The convertExceptionToResponse method gets right such errors that cause the 500 status.