Laravel会话不是存储新项目,但它的覆盖

发布于 2025-01-25 05:28:41 字数 1228 浏览 5 评论 0原文

我试图将新项目添加到会话容器中,如果可用旧项目,我将把新项目推到容器中。现在,每当我添加新项目时,都会通过替换上一项来存储当前项目,在控制器中,我的功能在哪些问题中

添加新项目

public function addToCart($id)
{
    $product = Product::findOrFail($id);

    $oldCart = Session::has("cart") ? Session::get("cart") : null;

    $cart = new Cart($oldCart);

    $cart->addNewItem($id, $product);

    Session::put("cart", $cart);

    dd(Session::get("cart"));
}

在类中 强>

class Cart
{
    public $items = null;
    public $totalQty = 0;
    public $totalPrice = 0;

    public function __construct($oldCart)
    {
        if ($oldCart) {
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
        }
    }

    public function addNewItem($id, $item)
    {
        $storeNewItem["product_id"] = $item->id;
        $storeNewItem["product_name"] = $item->product_name;
        $storeNewItem["product_price"] = $item->product_price;

        $this->totalQty++;
        $this->totalPrice += $item->product_price;
        $this->items[$id] = $storeNewItem;
    }
}

I was trying to add new items to a session container, and if old items are available then I'll just push the new items to the container. Now whenever I'm adding new items it's storing the current item by replacing the previous item, where is the issue?

In Controller my function to add new items

public function addToCart($id)
{
    $product = Product::findOrFail($id);

    $oldCart = Session::has("cart") ? Session::get("cart") : null;

    $cart = new Cart($oldCart);

    $cart->addNewItem($id, $product);

    Session::put("cart", $cart);

    dd(Session::get("cart"));
}

In Class

class Cart
{
    public $items = null;
    public $totalQty = 0;
    public $totalPrice = 0;

    public function __construct($oldCart)
    {
        if ($oldCart) {
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
        }
    }

    public function addNewItem($id, $item)
    {
        $storeNewItem["product_id"] = $item->id;
        $storeNewItem["product_name"] = $item->product_name;
        $storeNewItem["product_price"] = $item->product_price;

        $this->totalQty++;
        $this->totalPrice += $item->product_price;
        $this->items[$id] = $storeNewItem;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-02-01 05:28:41

购物车类中

public function addNewItem($id,$item) {
    
    $storeNewItem["product_id"] = $item->id;
    $storeNewItem["product_name"] = $item->product_name;
    $storeNewItem["product_price"] = $item->product_price;

    $this->totalQty++;
    $this->totalPrice += $item->product_price;

    return [
             'totalQty', => $this->totalQty,
             'totalPrice', => $this->totalPrice,
             'items[$id]', => $storeNewItem,
           ]

}

在控制器addtocart函数中的

$newCart = $cart->addNewItem($id,$product);

Session::put("cart",$newCart );

In Cart Class

public function addNewItem($id,$item) {
    
    $storeNewItem["product_id"] = $item->id;
    $storeNewItem["product_name"] = $item->product_name;
    $storeNewItem["product_price"] = $item->product_price;

    $this->totalQty++;
    $this->totalPrice += $item->product_price;

    return [
             'totalQty', => $this->totalQty,
             'totalPrice', => $this->totalPrice,
             'items[$id]', => $storeNewItem,
           ]

}

In Controller addToCart Function

$newCart = $cart->addNewItem($id,$product);

Session::put("cart",$newCart );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文