Undefined variable: errors in Laravel

LaravelLaravel 5Laravel ValidationLaravel MiddlewareLaravel 5.2

Laravel Problem Overview


When I want to register a user in my laravel project, the page always says >Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)"

According to the Laravel documentation, $errors should always automatically be set:

> So, it is important to note that an $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used.

I have this on on every view when I use:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

or any other way when I want to use the $errors variable.

Why is this? I never had this problem before.

Can someone help me please?

Laravel Solutions


Solution 1 - Laravel

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 :

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.

Solution 2 - Laravel

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.

Solution 3 - Laravel

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

Solution 4 - Laravel

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.

Solution 5 - Laravel

I was seeing this error too and later realised I had used the WithoutMiddleware trait as a means to bypass authentication for this particular test, but it ended up removing the validation error binding too. So I had to stop using the trait to keep the views working.

Solution 6 - Laravel

Go to App\Http\Kernel.php file. Move all the things of $middlewareGroups properties to $middleware.

Check for more details- http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/

Solution 7 - Laravel

count is not really realiable since it assumes the variable already exists. change the condition check to: @if($errors->has()) or just @if($errors)

Also if you are redirecting make sure to use this in your controller

return redirect()->back()->with('errors', $validator->messages());

EDIT: seen now that you are using L5.2 This may answer your question - you need to put your Routes in Route group.

https://stackoverflow.com/questions/34420590/laravel-5-2-validation-errors

Solution 8 - Laravel

protected $middleware = [              \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Social\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Social\Http\Middleware\VerifyCsrfToken::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        
    ],

    'api' => [
        'throttle:60,1',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.

make your kernel look like this

Solution 9 - Laravel

Your problem will be fixed by using this method.

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

If this doesn't help you, just run the following artisan command in addition to the above code:

php artisan key:generate

I solved in this way while using 5.2.*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnhingaView Question on Stackoverflow
Solution 1 - LaravelMarcin NabiałekView Answer on Stackoverflow
Solution 2 - LaravelFelipe PeñaView Answer on Stackoverflow
Solution 3 - LaravelKrzysztof BoduchView Answer on Stackoverflow
Solution 4 - LaravelTom11View Answer on Stackoverflow
Solution 5 - LaravelNikushView Answer on Stackoverflow
Solution 6 - LaraveltisuchiView Answer on Stackoverflow
Solution 7 - LaravelVojkoView Answer on Stackoverflow
Solution 8 - LaravelAmit RaiView Answer on Stackoverflow
Solution 9 - LaravelAsadullah Al GalibView Answer on Stackoverflow