Skip to content Skip to sidebar Skip to footer

Why Is My $items Doesn't Hold A Group Of $item? Laravel 8 Ecommerce

I'm from the same post 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)

Solution 1:

I think you need to re-define your add function in the Cart class to be like this, there are some syntax errors, (remove $ sign form $this->$items), and there is no need to re-declare $cart & $items in the function

publicfunctionadd($item, $id)
    {
        //default values if item not in cart$storedItem = [
            'qty' => 0,
            'price' => $item->price,
            'item' => $item
        ];

        //if item is already in shopping cartif (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;
    }

Solution 2:

The error you get is because of the way you try to get a property from $this cart object.

Change in line 39 (inside the add method):

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

To:

$this->items

Because in the way you currently have it defined, it tries to access a property on $this that has the value of the variable $items, inside your conditional.

This is also the reason why the error points to Cart::$ since items as a variable holds no value at that point in time.

Post a Comment for "Why Is My $items Doesn't Hold A Group Of $item? Laravel 8 Ecommerce"