Tuesday, March 21, 2017

Laravel session flash data sometimes not stored

Situation is very weird. Have two actions in my controller:

function setFlash()
{
   \Session::flash('test', 'hello');
   return redirect('/dumpSession');
}

function dumpFlash()
{
   print_r(\Session::all());
}

So, when I call site.local/setFlash, I am going to be redirected to site.local/dumpFlash, and can see dump result. So, interesting thing is that sometimes I see flash data in response, sometimes - not (every time I request /setFlash ofc). This is unpredictable - can see flash data 10 responses in row, then again no flash data for 1 - 3 responses.

It happens only with database session driver (file driver works perfectly, but not suitable for me). And only on local env (prod with same php / postgres version)).

And the most strange thing of that, I know what to write to fix this (dont ask me how I come to that) If I add some here code, to insert data to DB \App\Providers\AppServiceProvider::boot

public function boot()
{
    $item = new \App\Models\Item();
    $item->text = 'text';
    $item->save();
    ...
}

It fixes the problem. Of course it is not a solution. Where is the problem may be? When I have an insert to db - everything works fine. Looks like flash data sometimes not persists to db.



via Cyrus Smith

Advertisement