2

I have the following code where I loop through the items in the session array and change the value. How can I save it back to the session?

foreach(Session::get('cart.program') as &$item) {
    if ($item['id'] == '1xxx') { 
        item['id'] = '2xxx';
        break;
    }
}
1
  • @peterm I posted a new question. Hope this makes sense. Commented Aug 7, 2015 at 22:38

2 Answers 2

2

One way of doing it

$cart = Session::get('cart.program');

foreach($cart as &$item) {
    if ($item['id'] == '1xx') { 
        $item['id'] = '2xx';
        break;
    }
}

Session::put('cart.program', $cart);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I thought there would be a way of modifying and storing just one item in the array but this works.
1

Use Session::put() to save to the session in Laravel:

foreach(Session::get('cart.program') as $item){
    if ($item['id'] == '1xxx') { 
        Session::put('cart.program.id', '2xxx');
        break;
    }
}

2 Comments

This doesn't update $item['id']. It adds "id" => "2xxx" as a new item to cart['program']
I've seen the Laravel docs on working with session data but I'm not sure about the syntax for updating the value of item in the loop.

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.