SoHo
SoHo

Reputation: 699

Save new value to Laravel session array

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;
    }
}

Upvotes: 2

Views: 1467

Answers (2)

peterm
peterm

Reputation: 92845

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);

Upvotes: 2

Juan Serrats
Juan Serrats

Reputation: 1358

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;
    }
}

Upvotes: 1

Related Questions