Well, in Laravel documentation said this:
Custom HTTP Error Pages
Laravel makes it easy to return custom error pages for various HTTP
status codes. For example, if you wish to customize the error page for
404 HTTP status codes, create a resources/views/errors/404.blade.php.
This file will be served on all 404 errors generated by your
application.
The views within this directory should be named to match the HTTP
status code they correspond to.
I think you have to create a 401.blade.php view and then your code will work.
As showed here: https://stackoverflow.com/a/29633624/4425719
Extend Laravel's Exception Handler,
Illuminate\Foundation\Exceptions\Handler, and override
renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException
$e) method with your own.
If you haven't run php artisan fresh, it will be easy for you. Just
edit app/Exceptions/Handler.php, or create a new file.
Handler.php
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
class Handler extends ExceptionHandler {
// ...
protected function renderHttpException(HttpException $e) {
$status = $e->getStatusCode();
if (view()->exists("errors.{$status}")) {
return response()->view("errors.{$status}", compact('e'), $status);
}
else {
return (new SymfonyDisplayer(config('app.debug')))->createResponse($e);
}
}
}
And then, use $e variable in your 404.blade.php.
i.e.
abort(404, 'Something not found');
and in your 404.blade.php
{{ $e->getMessage() }}
For other useful methods like getStatusCode(), refer
Symfony\Component\HttpKernel\Exception
try.. catch