I have a code for cart using session. In shopping cart page I assigned the variable to session for fetch the cart and show it but when I change some values of variable, my session changed automatically and in next refresh of page, my session values updated.
I'm using laravel 5.
And this is my code:
$oldCart = Session::get('cart');
foreach($oldCart as $cartRow):
foreach($cartRow as $crKey => $row):
$oldCart->totalPrice -= 2000;
$oldCart->items[$crKey]['price'] -= 1000;
endforeach;
endforeach;
In code above, The value of 'total price' in session decreases 1000 in each refresh of page but I don't want to change any value of session.
You are working on a reference and when you change value you also modify the session object (and it changes the session).
Try to clone the object before you modify it.
$oldCart = clone Session::get('cart');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments