mardi 29 décembre 2020

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 App\Models;

class Cart
{
    private $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 (isset($this->$items) ? $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;
    }
}

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

in cart.blade.php

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire