Undefined variable: errors in Laravel
Asked Answered
S

9

70

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?

Simulacrum answered 24/12, 2015 at 14:12 Comment(5)
What version of Laravel? Specifically, in your composer.lock file, what version is it pulling?Refulgent
Interesting. I haven't messed with 5.2 yet, but I can clearly see in the documentation what you are saying. Might be a bug in 5.2? laravel.com/docs/5.2/validationRefulgent
Yes, Laravel 5.2 has bug. Many people have this same problem with Laravel 5.2Durant
check out this question: #34421090Rahel
check out this answer https://mcmap.net/q/281149/-undefined-variable-errors-in-custom-laravel-packageCopier
E
132

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.

Embed answered 24/12, 2015 at 14:31 Comment(6)
It's already in there... Copy-pasted the whole block to be sure, but it still doesn't workSimulacrum
Have you updated app from older Laravel release or you start new app from scratch in Laravel 5.2 ?Ladd
Started from scratch via scotchboxSimulacrum
Can you explain the first option a little bit more? Or where I should do this? I used your second option ('\illuminatte\...::class', into '$middleware') This solves the error, but now it gives me a new error: >Session store not set on request. Or what should I do to fix this one? Already thanks a lot!Simulacrum
@Simulacrum Ok, so you should put also in $middleware also others from web group - EncryptCookies , AddQueuedCookiesToResponse , StartSession and VerifyCsrfToken (in same notation as in file Kernel I put link). At the moment you don't have session started so you are getting this errorLadd
Great, this works! Thanks! Now the problem is that I can only make 1 DB call... I have to restart my sotchbox every time I make a call.. I can only check the phpmyadmin page when I restart scotchbox, and can only log in or register 1 user. After that, laravel doesn't find the DB anymore. (Also php my admin gives a 404 error after 1 call that)Simulacrum
M
20

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.

Marchant answered 28/12, 2015 at 1:3 Comment(1)
Yeah that was the same issue with me too. I had created the home(/) route outside the web middlewareBoucher
L
5

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

Lackluster answered 19/5, 2016 at 11:53 Comment(0)
A
4

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.

Anoint answered 20/2, 2016 at 18:15 Comment(2)
What? Cache needs to be cleared, in order middleware to working well.Anoint
Sorry, I don't know what I was thinking there.Koblenz
E
3

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.

Estop answered 2/1, 2016 at 12:17 Comment(2)
Thanks, this was my problem, too. Did you find a solution for testing stuff which has both the error variable and needs the WithoutMiddleware aswell?Amigo
@Amigo Unfortunately I didn't. In my tests I just created a user and logged them in for each test. Fortunately laravel makes this possible with very little code so I don't mind it.Estop
H
3

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/

Haema answered 23/3, 2016 at 5:45 Comment(0)
B
2

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.

Laravel 5.2 validation errors

Bunn answered 24/12, 2015 at 21:23 Comment(7)
No, it checks for existance EDIT: if you are speaking of my controller suggestion, that part is only if for example - validation fails.Bunn
not really... where does it check for existance? $errors->has() ? assumes that $errors variables existsRahel
If I'm not mistaken, has() method is boolean, so it will return 0 or 1 just like if you do if($errors)Bunn
To be more clear. Count needs to check inside of a variable $errors to find out how many items are there in array. has() method only return true or false without checking the variable. Hence, thats why it is method. I'm using my error handling in laravel like this since L4 and it works without a single problem aswell on my L5.1Bunn
I think you misanderstand something... $errors->has() means that a function called $has() is called on $errors. If $errors does not exists ( or is null or does not define has() function ) this will fail. Try it with $notexistant->has(). that will fail.Rahel
Laravel always has $errors. The question is is it an array or empty var.Bunn
Nevermind - saw now that question was about L5.2 - it is middleware problem.Bunn
H
2

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.*

Hochheimer answered 15/9, 2018 at 21:23 Comment(0)
J
0
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

Jaggy answered 31/3, 2016 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.