withErrors() - not working

Posted 2 years ago by AnthonyP

Hi, getting up and running with Laravel, and I've run into a very frustrating issue. It seems that ->withErrors() is failing inside of the "web" middleware group, and the session $errors variable is always empty after the redirection. I'm at my wit's end, hoping someone more experienced can help me out.

When I place this code (and only this code) in my routes.php file, when I visit /makeerrors it redirects correctly to /showerrors, but the errors are not in the Session (nor in the $errors variable in a view), even though the 'test' Session variable has been passed successfully, so the session seems to be working correctly.

Route::group(['middleware' => 'web'], function () {
    Route::get('showerrors', function(){
        dd( Session::get('errors'), Session::get('test'), Session::all() );
    });
    Route::get('makeerrors', function(){
        $validator = Validator::make(['title' => '', 'body' => '' ], [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
        Session::put('test', 'test 1234' );
        return redirect('showerrors')
            ->withErrors($validator);
    } );
});

However, if I comment out the 'web' middleware group, the errors appear as intended.

// Route::group(['middleware' => 'web'], function () {
    Route::get('showerrors', function(){
        dd( Session::get('errors'), Session::get('test'), Session::all() );
    });
    Route::get('makeerrors', function(){
        $validator = Validator::make(['title' => '', 'body' => '' ], [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
        Session::put('test', 'test 1234' );
        return redirect('showerrors')
            ->withErrors($validator);
    } );
// });

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.