Undefined variable: errors in Laravel

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 be automatically set:

So, it is important to note that the $ errors variable will always be available in all your views for each request, which allows you to conveniently assume that the $ errors variable is always defined and can be used safely.

I have this 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 in any other way when I want to use the $errors variable.

Why is this? I didn’t have this problem before.

Can anybody help me?

+46
laravel laravel-5 laravel-validation laravel-middleware
Dec 24 '15 at 14:12
source share
8 answers

You have to make sure that in app/Http/Kernel.php in middlewareGroups 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 the route you are using or put \Illuminate\View\Middleware\ShareErrorsFromSession::class, in the $middleware properties array

or

Inside the routes.php file, try creating your routes in the next block

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

UPDATE FOR NEW LARAVEL APPLICATION VERSIONS

Remember that you may run into problems if you use web middleware twice. A change was made to the Laravel 5.2.27 application (do not confuse it with the Laravel base that you are currently using - you can use the Laravel framework, for example 5.2.31, but have a Laravel application in version 5.2. 24), in which the web intermediate software is applied automatically for all routes. Therefore, in case of problems, you should open the app/Providers/RouteServiceProvider.php and check its contents.

You can compare it here too:

In case you have a newer version (which applies web middleware automatically), you should no longer use the web middleware in routes.php , or you should change your RouteServiceProvider method RouteServiceProvider that you do not use the group web middleware, Otherwise If this provider automatically uses the middleware group web , and you use it also in routes.php , you can get very unexpected results.

+76
Dec 24 '15 at 2:31 on
source share

I had the same problem with Laravel 5.2.x.

Inside the routes.php file routes.php try creating routes in

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

statement.

+17
Dec 28 '15 at 1:03
source share

I had a similar problem and solved this problem by adding routes to the middleware properties array,

BUT

it only worked after calling php artisan route:cache (flushing the route cache) afterwards.

I hope some of you find this helpful.

+3
Feb 20 '16 at 18:15
source share

I also saw this error, and later realized that I used the WithoutMiddleware attribute as a means of bypassing authentication for this particular test, but in the end it also removed the verification error binding. Therefore, I had to stop using this trait in order to maintain my views.

+2
Jan 02 '15 at 12:17
source share

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

Check additional information - http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/

+2
Mar 23 '16 at 5:45
source share

Also remember: if you are writing tests and your variable has $ errors, make sure that you are not using the Without Magic trait.

+2
May 19 '16 at 11:53
source share

count is not really feasible since it is assumed that the variable already exists. change the status check to: @if($errors->has()) or just @if($errors)

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

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

EDIT: now you see that you are using L5.2 This may answer your question - you need to place your routes in a group of routes.

Laravel 5.2 validation errors

0
Dec 24 '15 at 9:23
source share
 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 route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ ], 'api' => [ 'throttle:60,1', ], ]; /** * The application route middleware. * * These middleware may be assigned to groups or used individually. 

make your kernel look like this

0
Mar 31 '16 at 21:02
source share



All Articles