Update session array
Hello,
I have a little problem :/ i want update my session array, but idk how
i have this : $productsessions = Session::get("products_session");
This return :
array:4 [
0 => array:2 [
"product_id" => 16
"quantity" => 0
]
1 => array:2 [
"product_id" => 17
"quantity" => 0
]
2 => array:2 [
"product_id" => 20
"quantity" => 0
]
3 => array:2 [
"product_id" => 22
"quantity" => 0
]
]
But on my controller i want for exemple update quantity when product_id = xx
i try this :
foreach ($productsessions as $productsession) {
if ($productsession['product_id'] == $product_id) {
$productsession['quantity'] = 10;
}
}
but that dont work
Ty for your help
$productsSession = Session::get('products_session', []);
foreach ($productsSession as $productSession) {
if ($productSession['product_id'] == $product_id) {
$productSession['quantity'] = 10;
}
}
Session::set('products_session', $productsSession);
You can also use session helper
$productsSession = session('products_session', []);
session(['products_session' => $productsSession]);
You have to again save session if you want to update it.
Please or to participate in this conversation.