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

octoxan's avatar

Laravel not saving session

On one page I have a form that is submitted via ajax. I'm trying to save the forms inputs values to the session whenever the filter is ran.

Right now I have this in the controller...


if (count($request->session()->get('filters')) > 0) {
            $filters = $request->session()->pull('filters');
            var_dump($filters);
            foreach ($filters as $filter => $value) {
                $request->$filter = $value;
            }
            $request->session()->put('filters', $request->all());
        }

which the var_dump shows that it contains all the selected filters. Then on another page I'm just returning the session, for debugging purposes.

Further down in the controller, for that page I have...

return $request->session()->get('filters');

...and it'll usually return the filters from the first time I search, but then it never updates them. I've tried doing $request->session()->forget('filters'); before putting them back in the session, but it's not updating.

So let's say I search for "foo", the page that returns the filters initially returns this...

{
  "page": "1",
  "name": "foo"
}

...but then let's say I add another filter called "date" and hit submit... the filter functions and narrows the results, and var dumps this...

array (size=5)
  'page' => string '1' (length=1)
  'date' => string '2014-01-01' (length=10)
  'name' => string 'foo' (length=5)

However, the other page still returns the original results. How can two pages return different results for $request->session()->get('filters')?

All I'm trying to do is save all the filters values to the session, so when a user comes back to the page after browsing a bunch of other pages, the filters are still filled/applied.

0 likes
1 reply
ejdelmonico's avatar

I think I understand what you are trying to do, save data to the session. When you save data to the session through the request, you are pulling that data out of the session when using the pull() method. In short, the session no longer contains that data.

Could it be that when you try to view the data in the second page that it is not refreshed? I would use a dd($request->session()->get('filters')` in the second page to make sure it is correct. If its not, then maybe it is dafe to assume that the session is not being update in the second page.

Please or to participate in this conversation.