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

skcin7's avatar

Sessions are working fine, but flash data is not working at all? Help!

In my Laravel 5.2 application I have a very peculiar error. My Session variables are storing and working perfectly, but my Session Flash Data does not seem to work no matter what I try. I've tested this on both my local development machine and live production server, and Session Flash Data is not working in both places. I have other Laravel 5.1 and 5.2 projects on both machines and never encountered anything like this. Here's my code:

Route::group(['middleware' => ['web']], function () {

    // If I go here, it redirects to 'session/result', but on that page it always
    // outputs: "No value in Session flash_message found"
    // So, it's not detecting the flash_message on the redirected page! :(
    Route::get('session/test-flash', function() {
        Session::flash('flash_message', 'This is my flash message!');
        return redirect('session/result');
    });

    // I made this just to test that Sessions are indeed working PROPERLY
    // Which is so strange that Sessions are working fine, but
    // Flash Data is not?
    Route::get('session/test-set', function() {
        Session::set('flash_message', 'This is my flash message!');
        return redirect('session/result');
    });

    // This is the page that it redirects to, which checks to see if a
    // session variable is present. In this example,
    // Session::has('flash_message') is failing when redirecting from
    // after setting a flash_message. Please help me figure out why!
    Route::get('session/result', function() {
        if(Session::has('flash_message'))
            echo 'Session "flash_message" variable is: ' . Session::get('flash_message');
        else
            echo 'No value in Session flash_message found';
    });

    // I'm just using this to reset Session data to help test
    Route::get('session/flush', function() {
        \Session::flush();
        die('Session flushed.');
    });

});

If I go to http://myapp/session/test-flash, it redirects to 'session/result', and it always fails to show the flash_message - it says "No value in Session flash_message found" no matter what I try! However, if I go to http://myapp/session/test-set, it does successfully set my Session variable after it redirects to 'session/result'. So, Sessions ARE working properly, just not Flash Data. :(

I'm not sure what to do here! I'm ripping my hair out. I've even gone so far as to do things like chmod -R 777 storage, php artisan key:generate, and other things that I don't think would work but out of desperation would hope it fixes it. I've even given the code to a Laravel friend through IRC (shoutouts to lagbox) and it's working fine on his machine, just not mine! :(

Please if you can provide any insight on how to solve this problem so that my Session Flash Data will work properly, or help me figure out what I might be doing wrong, it would be greatly appreciated.

0 likes
9 replies
h0lend3r's avatar

Can you use with method to flash your data and tell if it's working. Replace this

Session::flash('flash_message', 'This is my flash message!');

With this

return redirect('session/result')->with('flash_message', 'This is my flash message!');

You can also check your logs file and check if something is wrong

h0lend3r's avatar
h0lend3r
Best Answer
Level 3

@skcin7 what version of laravel are you using? It could be that your routes have web middleware applied twice. Try to delete this middleware from your routes.php file

1 like
skcin7's avatar

Okay, interesting. I added dd(Session::has('flash_message')) into these methods. The result of that is true before the redirect(), and false after the redirect. I have no idea why, because the flash_message variable should remain after the redirect() happens.

Route::get('session/test-flash', function() {
    Session::flash('flash_message', 'This is my flash message!');

    // This returns `true` when I uncomment it out
    // dd(Session::has('flash_message'));

    // return redirect('session/result')->with('flash_message', 'This is my flash message!');

    return redirect('session/result');
});

Route::get('session/result', function() {

    // This returns `false` when I uncomment it out.
    // dd(Session::has('flash_message'));

    if(Session::has('flash_message'))
        echo 'Session "flash_message" variable is: ' . Session::get('flash_message');
    else
        echo 'No flash_message found';
});
skcin7's avatar

@h0lend3r , I'm on Laravel 5.2. Okay, I just removed the 'web' middleware and now it works perfectly! Thank you!

But...... why?! I have other Laravel 5.2 applications, and Sessions wouldn't work at all unless I put everything under the 'web' middleware in my routes.php file. What's different about this application than other 5.2 applications I have running on my machine, that this one doesn't require the 'web' middleware to be applied in my routes?

You said the web middleware may be applied twice. If so, where/how else is it being applied?

skcin7's avatar

From https://laravel.com/docs/5.2/middleware#middleware-groups"

Keep in mind, the web middleware group is automatically applied to your default routes.php file by the RouteServiceProvider.

Looks like the 'web' middleware is automatically applied to all Routes in RouteServiceProvider. In the past in Laravel 5.2 applications, we were required to add the 'web' middleware within routes.php, for things like Sessions to work. Now, it seems that's happening automatically.

@h0lend3r , THANK YOU, I want to kiss you right now.

woltenx's avatar

In my laravel 7.2 i have same problem, i resolve using session::put()...

         session()->put('error','TEST');
         return redirect()->back();                        #->with('error','TEST');

Please or to participate in this conversation.