Undefined variable: errors in Laravel

Also to be aware of: If you write tests and your view has $errors variable make sure you don't use WithoutMiddleware trait.


I had this very same issue with Laravel 5.2.x.

Inside of the routes.php file try yo create your routes within the

Route::group(['middleware' => ['web']], function () {
    //routes here
}

statement.


I had similar problem and solved this one by adding routes into middleware property array as well,

BUT

it worked only after calling php artisan route:cache (clearing route cache) subsequently.

I hope some of you would find this useful.


You should make sure that in app/Http/Kernel.php in middlewareGroups property for web you have:

\Illuminate\View\Middleware\ShareErrorsFromSession::class,

in this array. Compare this with https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php

EDIT

It seems you need to add 'middleware' => 'web' for route you are using or put \Illuminate\View\Middleware\ShareErrorsFromSession::class, into $middleware property array

or

Inside of the routes.php file try to create your routes within the following block

Route::group(['middleware' => ['web']], function () {
    //routes here
});

UPDATE FOR NEWER VERSIONS OF LARAVEL APPLICATION

Be aware that you might run into problems also in case you use web middleware twice. There was a change in Laravel application 5.2.27 (don't confuse it with Laravel framework you use at the moment - you might use Laravel framework for example 5.2.31 but have Laravel application in version 5.2.24) in which web middleware is applied automatically for all routes. So in case of problems, you should open your app/Providers/RouteServiceProvider.php file and verify its content.

You can compare it also here :

  • RouteServiceProvider for Laravel application 5.2.24

  • RouteServiceProvider for Laravel application 5.2.27

In case you have newer version (that applies web middleware automatically), you shouldn't use web middleware in routes.php anymore or you should modify your RouteServiceProvider method to not apply web group middleware. Otherwise if web middleware group is automatically applied in this provider and you use it also in routes.php you might get very unexpected results.