【发布时间】:2017-11-15 05:16:01
【问题描述】:
我有两个项目需要在会话中更新一些值。它适用于我的另一个项目,所以我在我的新项目中使用了相同的代码(相同的 laravel 版本)。现在它不再起作用了,我不知道为什么。我没有收到任何错误消息,例如一切是否正常,但没有。
以下是我会话中项目的一些虚拟数据:
articleNr: "16xxxxxx"
pictureUrl: "http://blablablabla...."
title: 'chocolate'
count: 7 // thats the amount - So I want 7x chocolate
代码:
$product_cartData = $request['product_data']; // In product data is the articlenumber and the amount I want to update ( like if I want to buy 5x this item )
$all_products = $request->session()->get('products');
foreach ($product_cartData as $data) {
foreach ($all_products as $key => $sProduct) {
if ($sProduct['articleNr'] == $data['id']) {
$sProduct['count'] = (int)$data['quantitie'];
}
// return $sProduct; <--- this returns the item with the updated quantitie
}
}
return session('products', $all_products); // <--- but here the quantitie isn't updated anymore
有人知道为什么吗?这正是我在其他项目中所做的并且工作正常。
感谢您的帮助!
【问题讨论】:
-
试试你的 foreach 就像
foreach ($all_products as $key => &$sProduct) -
什么都没有改变:/