Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating previous Session Array Laravel

I have an issue on how can I update my Previous array ? What currently happening to my code is its just adding new session array instead of updating the declared key here's my code:

foreach ($items_updated as $key => $added)
{
    if ($id == $added['item_id'])
    {
        $newquantity = $added['item_quantity'] - 1;
        $update = array(
            'item_id' => $items['item_id'],
            'item_quantity' =>  $newquantity,
        );
    }
}

Session::push('items', $updated);
like image 995
Martney Acha Avatar asked Dec 25 '22 06:12

Martney Acha


1 Answers

$items = Session::get('items', []);

foreach ($items as &$item) {
    if ($item['item_id'] == $id) {
        $item['item_quantity']--;
    }
}

Session::set('items', $items);
like image 55
Joseph Silber Avatar answered Dec 27 '22 20:12

Joseph Silber