Just to expand the existing answer a bit: be sure to remove this middleware from $ middlewareGroups, if you have one, so you don't use middleware twice.
You end up with something like this:
protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, ]; protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
As far as I understand, this was due to the fact that the session-related middleware, while in a web group, was applied only to those pages that were redirected to web.php. And since error handling is not redirected to the routed page by default, we did not have access to the session.
Thus, the middleware will apply to all pages, not just the routes on the web page, including errors.
I initially found soution here , but it took me a while to figure out why this was happening (I thought I caught a cold, ruined everything, feel free to confirm or fix this, please).
Hope this helps, it works for me on Laravel 5.4
lluchmk
source share