Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AnthonyP's avatar

withErrors() - not working

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);
    } );
// });
0 likes
8 replies
Jaytee's avatar

I used your exact same code except returned a view (I also tried without a view) and I got an output with the 'web' middleware.

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

Showerrors view:

@foreach($errors->all() as $errors)
    <li>{{ $error }}</li>
@endforeach

Have you by any chance modified any of your Middleware?

EDIT: I did a test as I had a feeling and I believe i may have found the answer to your issue. I found that having two 'web' middleware groups stopped the errors from showing.

Example:

Route::group(['middleware' => 'web'], function() {
    Route::group(['middleware' => 'web'], function() {
        // Doesn't work/
    });
});

If you have a layout like that, remove one of the web routes.

AnthonyP's avatar

Thanks for the response! Unfortunately, I have not modified any middleware, nor do I have two 'web' middleware groups.

To make absolutely sure, I made a new directory on my computer and ran composer create-project laravel/laravel testlaravel "5.2.*". The issue, exactly as I described earlier, exists on a brand-new laravel project, when I have changed none of the defaults.

I am on Mac OSX, running MAMP, PHP 5.6.*. Any other ideas? I might attempt to run homestead and see if it is some problem with the environment.

Snapey's avatar

On the very latest release, web middleware is include by RouteServiceProvider. Have you php Artisan route:list and checked the table?

AnthonyP's avatar

Snapey, I'm not sure I follow. Here is the result of running php artisan route:list

+--------+----------+------------+------+---------+------------+
| Domain | Method   | URI        | Name | Action  | Middleware |
+--------+----------+------------+------+---------+------------+
|        | GET|HEAD | makeerrors |      | Closure | web,web    |
|        | GET|HEAD | showerrors |      | Closure | web,web    |
+--------+----------+------------+------+---------+------------+
AnthonyP's avatar

Oh! That's interesting, I just compared that to the output without manually running the web middleware group, and it appears the web middleware is running twice.

+--------+----------+------------+------+---------+------------+
| Domain | Method   | URI        | Name | Action  | Middleware |
+--------+----------+------------+------+---------+------------+
|        | GET|HEAD | makeerrors |      | Closure | web        |
|        | GET|HEAD | showerrors |      | Closure | web        |
+--------+----------+------------+------+---------+------------+

Could you please explain how web middleware gets run twice??

Jaytee's avatar

Hmm, glad you sorted it anyway but maybe it's added into all routes by default now.

I haven't upgraded to the latest hence why i didn't receive an error with the errors. Check the middlewares and RouteServiceProvider to see if it's been added :)

Snapey's avatar

yes, a change last week on Master to include web middleware in every route. No need to refer to it in routes.php anymore.

It's still in the process of being changed over so artisan make:Auth still erroneously adds it to routes.php.

AnthonyP's avatar

That was the confusing part - I installed laravel per the docs, and also used make:auth per the docs, and it was broken.

You guys are the best, thank you so much.

Please or to participate in this conversation.