JaredDev Logo

Laravel: Flash Session Data Not Working

2017-09-13

Flashing Data to Session in Laravel 5.5 (most likely 5.2+)

In one of the projects I've been working on at work I finally was to a point where I needed to flash a message to the user in response to a form submission. No big deal...normally.

The issue...

For whatever reason myself and another one of our Senior Developers ended up spending an hour or so debugging why data flashed to the session would not persist through a redirect like it should have!

We combed the docs and searched online the entire time. Everything pointed to the middleware, but all the issues that people were posting code samples of just didn't apply. Searching online for Laravel flash session data not working returns a whole host of solutions so I won't repeat what is posted elsewhere (beyond what my issue was).

The Fix

Our error ended up being in the app\http\kernal.php. There are different sections where you can define your middleware classes. I ended up having the \Illuminate\Session\Middleware\StartSession::class class listed twice.

Once in...

protected $middleware = [
    ...
    \Illuminate\Session\Middleware\StartSession::class,
    ...
];

Then again in...

protected $middlewareGroups = [
    'web' => [
        ...
        \Illuminate\Session\Middleware\StartSession::class,
        ...
    ],
    ...
];

So, just remove one of those instances and all should be right with the world again!

tl;dr;...

...if you are having issues flashing data to the session check your middleware as I've yet to find a situation (while I'm sure it exists) where the middleware wasn't the culprit.