2

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?

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

or try this

$items = Session::pull('items');
$items[] = $id;
Session::push('items', $items);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.