为什么我的 $items 不包含一组 $item? Laravel 8 电子商务

Why is my $items doesn't hold a group of $item? Laravel 8 Ecommerce

I'm from the same post 当我删除 if($this->items) 中的 isset 函数时弹出 here but I've found the root problem why is my quantity won't increment but I don't know how to solve this. The problem is at my isset function (in Cart.php), because I tried to echo something there and just realized the function isn't running. This is the error。我也试过 dd($items),它说 null。为什么我的 $items 没有包含一组 $item?你们知道为什么以及如何解决这个问题吗?

p/s:从之前的 post 开始,我删除了我的 $oldCart 并将其替换为 $cart,我希望 $cart 能够覆盖自身。 这些都是与我的购物车相关的代码。

在Cart.php

<?php

namespace App\Models;

class Cart
{
    public $items = array();
    public $totalQty = 0;
    public $totalPrice = 0;


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


    public function add($item, $id)
    {
        global $cart;
        global $items;

        //default values if item not in cart


        $storedItem = [
            'qty' => 0,
            'price' => $item->price,
            'item' => $item
        ];


        //if item is already in shopping cart
        if ($this->$items) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }

        //dd($items);
        $storedItem['qty']++;
        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;
        $this->totalQty++;
        $this->totalPrice += $item->price;
    }
}

在FoodController.php

<?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Food;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;


class FoodController extends Controller
{
public function addtoCart(Request $request, $id, Food $foods)
    {

        $foods = Food::find($id);

        //check in session if cart already contains product
        $cart = Session::has('cart') ? Session::get('cart') : null;

        //dd($cart);
        //if it did contain products, pass them to constructor
        if (!$cart) {
            $cart = new Cart($cart);
        }

        $food = $foods->id;
        $cart->add($foods, $food);

        Session::put('cart', $cart);
        //dd($request->session()->get('cart'));


        return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
    }

    public function getCart()
    {
        if (!Session::has('cart')) {
            return view('cart');
        }

        $cart = Session::get('cart');
        $cart = new Cart($cart);
        return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
    }
}

在cart.blade.php

    @foreach ($food as $foods)
                        
                        
       <p align="left"><a href="#"> {{ $foods['item']['name'] }} </a> <span class="price">MYR {{ $foods['price'] }} </span></p>
       <p align="left">Quantity</p>
       <p><input class="w3-input w3-p-adding-16 w3-border" type="number" placeholder="Quantity" value="{{ $foods['qty'] }}" required name="quantity"></p>
                        
@endforeach
                        
                        
<hr>
                        
<p align="left">Total <span class="price"><b>MYR {{ $totalPrice }}</b></span></p>

您收到的错误是因为您尝试从 $this 购物车对象获取 属性 的方式。

第 39 行的更改(在 add 方法内):

if (isset($this->$items) ? $items : false)

收件人:

$this->items

因为按照您当前定义它的方式,它会尝试访问 $this 上具有变量 $items 值的 属性, 在你的条件中。

这也是错误指向 Cart::$ 的原因,因为作为变量的项目在那个时间点没有值。

我认为你需要重新定义你在购物车中的添加功能 class 是这样的,有一些语法错误,(删除 $ 符号形式 $this->$items) , 也不需要在函数中重新声明 $cart & $items

public function add($item, $id)
    {
        //default values if item not in cart


        $storedItem = [
            'qty' => 0,
            'price' => $item->price,
            'item' => $item
        ];

        //if item is already in shopping cart
        if (isset($this->items) ? $this->items : false) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }

        //dd($items);
        $storedItem['qty']++;
        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;
        $this->totalQty++;
        $this->totalPrice += $item->price;
    }