【问题标题】:Can't add multiple object to session in Laravel无法在 Laravel 中向会话中添加多个对象
【发布时间】:2019-09-02 20:44:37
【问题描述】:

当我尝试将商品添加到购物车时,它只会添加一次,如果我再次尝试添加它,它会覆盖它并且商品计数器保持为 1。

我尝试过从按钮切换到链接,我尝试过不同的路由,以及其他人解决类似问题的方法,我也尝试过使用 push 方法而不是 put(当我尝试将内容保存在session),但它不起作用,因为它是一个关联数组。而且我得出的结论是,问题很可能出在控制器文件中。

我的控制器

public function getAddToCart(Request $request, $id)
{
    $burgeri = Burgeri::find($id);
    $oldCart = Session::has('cart') ? Session::get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->add($burgeri, $burgeri->id);

    $request->session()->put('cart',$cart);
    return redirect()->route('burgeri.index');
}

购物车.php

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

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

    public function add($item, $id)
    {
        $storedItem = ['quantity' => 0, 'price' => $item->price, 'item' => $item];
        if ($this->items) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }
        $storedItem['quantity']++;
        $storedItem['price'] = $item->price * $storedItem['quantity'];
        $this->items[$id] = $storedItem;
        $this->totalQuantity++;
        $this->totalPrice += $item->price;
    }
}

The Blade View

<button class="cool_btn" onclick="window.location='{{route('burgeri.addToCart', ['id' => $burgeri->id]) }}'">
    Add to Cart
</button>

这就是我显示计算购物车中商品数量的计数器的方式。

<li class="nav-item">
    <a class="nav-link" href="{{ url('Cart') }}">
        <i class="fa fa-shopping-cart"></i>
        Cart
        <span class="badge badge-secondary">{{Session::has('cart') ? Session::get('cart')->totalQuantity : ''}}</span>
    </a>
</li>

当我使用:dd($request-&gt;session()-&gt;get('cart')); 方法时,我得到了正确的项目及其参数,但我不知道为什么计数器不工作,它以某种方式重置本地存储的购物车,这是我的猜测。

感谢您阅读本文,感谢您提供任何帮助。

【问题讨论】:

  • 如果条件,你正在覆盖 $storedItem
  • 虽然你发现你的构造函数写错了,你也在 if(array_key_exist) 条件中覆盖了 $storedItem 。 if (array_key_exists($id, $this->items)) { $storedItem = $this->items[$id]; }

标签: php html laravel laravel-5


【解决方案1】:

在函数回调中传递对象时,无需传递项目的 id

$cart->add($burgeri); 

然后停止在函数中覆盖$storedItem

public function add($item)
{
    $storedItem=['item'=>$item, 'quantity'=>1];
    if($this->items)
    {
       if(array_key_exists($item->id, $this->items))
       {
          $storedItem['quantity']++;
       }
    }
    $storedItem['price']=$item->price * $storedItem['quantity'];
    $this->items[$id]=$storedItem;
    $this->totalQuantity++;
    $this->totalPrice+=$item->price;
}

【讨论】:

    【解决方案2】:

    我很抱歉,但事实证明我写错了构造函数......它的构造不是构造函数, 其他一切都按预期工作

    【讨论】:

    • 您可以在您的问题下发表评论。无需发布答案。
    猜你喜欢
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2017-09-03
    • 2015-02-15
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多