Reputation: 5900
I am having trouble while updating session array value in laravel 5. Here is my function,
public function postCartItemAdd()
{
$id = Request::input('id');
Session::push('items', $id);
dd(Session::all());
}
Instead of pushing a new id into the array it just replaces the existing value leaving single item. Am I doing something wrong?
Upvotes: 2
Views: 3275
Reputation: 134
The problem is the session is saved as a flash data. So, you need to save the session whenever you push the data.
$request->session()->push('user.items', 'item1');
$request->session()->push('user.items', 'item2');
$request->session()->save();
Upvotes: 1
Reputation: 21901
umm i think you used it wrong,
see the DOC
it says
Session::push('user.teams', 'developers');
user
is the array and we gonna put a value developers
to that array with teams
key
so then you need to use it in your case as,
Session::push('items.id', $id);
OR if you need to maintain items
as an array with default keys like 0,1,2,3...
to put the ids
, then items
should be an array
so there should be a something like,
Session::put('items', []);
then you can use Session::push('items', $id);
if you need to push ids in to same array as you tried.
Upvotes: 0
Reputation: 3784
or try this
$items = Session::pull('items');
$items[] = $id;
Session::push('items', $items);
Upvotes: 0