Skip to content
Advertisement

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), because I tried to echo something there and just realized the function isn’t running. This is the error pops up when I removed isset function at the if($this->items). I tried to dd($items) too, and it said null. Why is my $items isn’t holding a group of $item? Do you guys know why and how to solve this?

p/s: from the previous post, I’ve removed my $oldCart and replace it with $cart, and I’m expecting $cart to overwrite itself. These are all the codes related to my shopping cart.

In Cart.php

<?php

namespace AppModels;

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;
    }
}

in FoodController.php

<?php

namespace AppHttpControllers;

use AppModelsCart;
use AppModelsFood;
use IlluminateHttpRequest;
use IlluminateSupportFacadesSession;


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]);
    }
}

in 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>

Advertisement

Answer

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

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;
    }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement