I'm currently storing and retrieving data through angular requests that puts and retrieves values from a Laravel Session Array.
// Code for pushing into Laravel Session Array
Session::push('cart', $object);
// Code for retrieving data into Laravel Session Array
return Session::get('cart');
Now i'm using a foreach loop to update quantity or where this question is about: To remove an object from the Laravel Session Array Cart when quantity is 0.
foreach ($cart as $index => &$product) {
if ($product['id'] == $id) {
if($operator == '+'){
$product['quantity'] = $product['quantity'] + $quantity;
} else if($operator == '-'){
$product['quantity'] = $product['quantity'] - $quantity;
if($product['quantity'] == 0){
Session::forget('cart.' . $index);
$cart = Session::get('cart', []);
Session::push('cart', $cart);
}
}
Session::set('cart', $cart);
$this->new_product = false;
break;
}
}
This part of the code above is altering my JSON output:
Session::forget('cart.' . $index);
$cart = Session::get('cart', []);
Session::push('cart', $cart);
JSON Situation Before Deleting:
[
{
"id":293,
"quantity":2
},
{
"id":294,
"quantity":2
}
]
JSON Situation After Deleting:
{"1":{"id":293,
"quantity":2}
}
But Should Be:
[
{
"id":293,
"quantity":2
}
]
This is my JSON Get Request which simply calls:
return Session::get('cart')
Cart.getCart()
.then(function (success){
$scope.cart = success.data;
}).catch(function (e){
console.log("got an error in the process", e);
});
Any help would be appreciated. Not sure what is causing my json array to transform into a different data model.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire