0

I keep having this error every time I try to validate a form:

Undefined variable: errors (View: /home/vagrant/Code/talents/resources/views/welcome.blade.php)

I tried modifying the kernel.php file, and also adding the route into the 'middleware' => 'web' in the routes file, but none of those seem to fix the problem.

Undefined variable: errors in Laravel

Community
  • 1
  • 1

2 Answers2

0

If you need access to session errors you need sessions. You should move your '/' route to the 'web' middleware group.

Route::group(['middleware' => ['web']], function () {
    Route::get('/', function () {
        return view('welcome');
    });        
});

The 'web' middleware group gives you sessions and the session errors.

If you have this setup at the moment and are still having that issue, you may want to change any adjustments you made to Kernel.php and use the default one. If you change the middleware applied to the 'web' group, you could cause an issue where things dont get applied correctly.

In regard to the session errors, those are being shared with views in \Illuminate\View\Middleware\ShareErrorsFromSession, which the 'web' middleware group has applied.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • It seems OP mentioned this solution doesn't work for him – Marcin Nabiałek Dec 31 '15 at 20:21
  • I fixed it. The problem was not the errors. It was a problem when passing the data back the data to the views return redirect()->back()->withErrors($validator); – Edgar Velazquez Dec 31 '15 at 21:28
  • That is odd, considering there is always $errors available to the views when the 'web' group is applied. If there isn't $errors in the session it adds an empty ViewErrorBag. – lagbox Dec 31 '15 at 21:40
  • the problem wasn't the errors. I have a problem with the function redirect(). when I use that function, it goes to the url but no data is being passed. i.e.: return redirect()->back ()->withErrors ($validator); it goes back to the url but no $validator nor Errors is being passed. – Edgar Velazquez Jan 01 '16 at 01:03
  • is the route where you are flashing those errors, in the 'web' group as well ? – lagbox Jan 01 '16 at 01:10
  • this is what I have: Route::post ('auth/register', ['as'=> 'postRegister', 'uses'=> 'AuthController@postRegister', 'middleware'=>['web']]); Route::get ('auth/register', ['as'=> 'register', 'uses'=> 'AuthController@getRegister', 'middleware'=>['web']]); and in my controller I check if the validator fails the this code runs return redirect ()->back ()->withErrors ($validator ); and in the view @if ($errors) – Edgar Velazquez Jan 01 '16 at 01:34
0

Solved

You may change any one of the following:

1. put your working route (app/http/routes.php) on

Route::group(['middleware' => ['web']], function () { // Here like Route::get('/', 'TodoController@index'); Route::post('/', 'TodoController@store'); });

See the image here

2. Move your protected $middlewareGroups web (app/Http/Kernel.php) on protected $middleware = []

See the image here