29

I tried running the following code:

Session::put('progress', '5%');

dd(Session::get('progress'));

This will show '5%' in the dump.

If I rerun the same page but comment out Session::put('progress', '5%'); so that only the dd() line is called, I get a null value instead of the 5% values stored in the previous page load.

Here is my sessions config, so I know it should be storing the data:

'driver' => 'native',
'lifetime' => 120,
'expire_on_close' => false,

Why is Laravel not storing the session data across page loads?

eComEvo
  • 11,669
  • 26
  • 89
  • 145
  • 4
    I believe it is because you are killing the script with `dd()` before Laravel finish its lifecycle the value is not stored into the session. Try putting a value without the `dd` to complete the full Laravel lifecycle. Then, after that, try to retrieve it. – Rubens Mariuzzo May 28 '14 at 16:50

5 Answers5

63

The problem is because you are killing the script before Laravel finishes its application lifecycle, so the values put in session (but not yet stored) got killed too.

When a Laravel application lifecycle starts, any value put in Session are not yet stored until the application lifecycle ends. That is when any value put in Session will be finally/really stored.

If you check the source you will find the same aforementioned behavior:

 public function put($key, $value)
 {
     $all = $this->all();

     array_set($all, $key, $value);

     $this->replace($all);
 }

If you want to test it, do the following:

  1. Store a value in session without killing the script.

    Route::get('test', function() {
        Session::put('progress', '5%');
        // dd(Session::get('progress'));
    });
    
  2. Retrieve the value already stored:

    Route::get('test', function() {
        // Session::put('progress', '5%');
        dd(Session::get('progress'));
    });
    
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
48

Rubens Mariuzzo's answer is very good. I just want to add that if you need the data to be stored immediately you could use the save method:

Session::put('progress', '5%');
Session::save();
Community
  • 1
  • 1
sergei
  • 803
  • 7
  • 12
  • Thanks Man. you saved my day. – Sainesh Mamgain Aug 05 '16 at 05:15
  • Does this work for you even if you call a "die()" right after? For some reason, even if I call Session::save(), it's the same result. I have to return the response in order to make it work for some reason. I'm using L5.2 and L5.3 – Thomas Cheng Oct 13 '16 at 19:20
  • 1
    Just noticed that this case does not work when the session driver is set to "cookies", because cookies are not saved immediately, and are queued up to send as a batch all together at the end of the request. That means, if you dd(), it's not gonna work! Sux0rs... – Thomas Cheng Oct 13 '16 at 19:37
1

I moved the session middleware

\Illuminate\Session\Middleware\StartSession::class

to the property $middleware in

app/Http/Kernel.php

In this case, you need to remove it from the web group ($middlewareGroups)

enter image description here

It helped me, I hope it helps you too

Alex Pilyavskiy
  • 140
  • 1
  • 3
0

For me, even after data has been stored to session properly:

    dd(Session::all());

returns nothing, but:

    print_r(Session::all());

returns all session data!

Alex
  • 81
  • 7
0

In my case I flashed the variable in one request and then put it into session in another request (with the same name).

Unfortunatelly, terminating method went through all the previously flashed properties and cleaned my newly created session property (it was flushed in previous request so laravel thought it was no longer required and couldn't tell it was newly created). I figured it out debugging Kernel->terminateMiddleware method. You can put a breakpoint in terminating method. At some stage it reaches Store->ageFlashData. This is the method responsible for deleting my property.

Artur Owczarek
  • 1,146
  • 1
  • 11
  • 22