Laravel - 如何更改会话数组中的键值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37402167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:51:21  来源:igfitidea点击:

Laravel - How to change value of key inside Session array

phplaravelsession

提问by Codearts

So I am building a cart and I am using Session to store data for guest users. Here is how I store the items.

所以我正在构建一个购物车,我正在使用 Session 来存储来宾用户的数据。这是我存储物品的方式。

Session::push('cartItems', [
    'product' = > $product,
    'size' = > $request['size'],
    'quantity' = > $request['quantity']
]);

If a guest user adds an item with the same size it should only add the chosen quantity to the cartItem['quantity']. Here is how I do that:

如果来宾用户添加了相同尺寸的商品,则应仅将所选数量添加到cartItem['quantity']. 这是我如何做到的:

foreach(Session::get('cartItems') as $cartItem) {
    if ($cartItem['product'] - > id == $product_id) {
        $isNewItem = false;
        if ($cartItem['size'] == $request['size']) {
            $cartItem['quantity'] += (int) $request['quantity'];
        } else {
            Session::push('cartItems', [
                'product' = > $product,
                'size' = > $request['size'],
                'quantity' = > $request['quantity']
            ]);
        }
    }
}

When I try to add a product when a product with the same size already exists in the cart it goes through that part of the code

当我在购物车中已经存在相同尺寸的产品时尝试添加产品时,它会通过该部分代码

if ($cartItem['size'] == $request['size']) {
    $cartItem['quantity'] += (int) $request['quantity'];
}

But the quantity of the $cartItemdoesn't change at all. How can I change it?

但是数量$cartItem根本没有变化。我怎样才能改变它?

采纳答案by Codearts

I found a solution by doing the following:

我通过执行以下操作找到了解决方案:

$cartItems = Session::get('cartItems', []);
foreach ($cartItems as &$cartItem) {
    if ($cartItem['product']->id == $product_id) {
        $isNewItem = false;
        if ($cartItem['size'] == $request['size']) {
             $cartItem['quantity'] += (int)$request['quantity'];
             $cartItem->save();
             Session::set('cartItems', $cartItems);
        } else {
            Session::push('cartItems', [
                'product' => $product,
                'size' => $request['size'],
                'quantity' => $request['quantity']
             ]);
        }
    }
}

I found this solution by doing some digging. Here is the question.

我通过一些挖掘找到了这个解决方案。这是问题

回答by Denis Mysenko

In your loop, $cartItem is a temporary loop variable - changing it doesn't affect the session value. Or in other words, you are not updating your session when the sizes are equal.

在您的循环中, $cartItem 是一个临时循环变量 - 更改它不会影响会话值。或者换句话说,当大小相等时,您不会更新会话。

Laravel has a convenience method push() for pushing to arrays - which you are using already, but unfortunately there is no array "update" method, so you would need to fetch the whole cart, update it as necessary and then set it again:

Laravel 有一个方便的方法 push() 用于推送到数组 - 您已经在使用它,但不幸的是没有数组“更新”方法,因此您需要获取整个购物车,根据需要更新它,然后再次设置它:

$cartItems = Session::get('cartItems');
$newItems = [];    

foreach ($cartItems as $cartItem) {
        if ($cartItem['product']->id == $product_id && $cartItem['size'] == $request['size']) {
                $cartItem['quantity'] += (int)$request['quantity'];
        } else {
                $newItems[] = [
                        'product' => $product,
                        'size' => $request['size'],
                        'quantity' => $request['quantity']
                ];
        }
}

Session::put('cartItems', $cartItems + $newItems);