Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 369
Anhinga Avatar asked Dec 24 '15 14:12

Anhinga


People also ask

How do you fix a undefined variable?

Fix Notice: Undefined Variable by using isset() Function This notice occurs when you use any variable in your PHP code, which is not set. Solutions: To fix this type of error, you can define the variable as global and use the isset() function to check if the variable is set or not.

Which is undefined variable?

An undefined variable in the source code of a computer program is a variable that is accessed in the code but has not been declared by that code. In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time.

How do I show laravel errors?

How to make Laravel show PHP errors. By default, the Laravel framework has an error handling feature. The debug option in the configuration file determines the error display at the user end. Usually, the Laravel configuration file that enables debugging is the config/app.


1 Answers

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.

like image 170
Marcin Nabiałek Avatar answered Oct 14 '22 21:10

Marcin Nabiałek