简体   繁体   English

如何取消设置 laravel session 阵列

[英]How to unset a laravel session array

Hi I'm trying to remove a product array inside my cart session when the quantity is 1 and user tries to remove it, simply unsets that.嗨,当数量为1并且用户尝试删除它时,我正在尝试删除我的购物车 session 中的产品阵列,只需取消设置即可。

Here is my code:这是我的代码:

public function remove($id)
{
    if (session('cart')) {
        foreach (session('cart') as $product) {
            if ($product['id'] == $id) {
                if ($product['qty'] == 1) {
                
                } else {
                    $product['qty'] = $product['qty'] - 1;
                }
            };
        }

        Session::put('cart', session('cart'));
        
        return redirect()->back();
    }
}

I tried to use Session::forget('cart.'$num) but having some more issues.我尝试使用Session::forget('cart.'$num)但还有一些问题。

Take your session edit it, and then re-set it:拿你的 session 编辑它,然后重新设置它:

$cart = session('cart');
array_forget($cart, $num);
session()->put('cart', $cart);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM