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

2022-07-02 00:00:00 php html laravel laravel-8 laravel-5

我来自同一个帖子here但我找到了根本问题,为什么我的数量不会增加,但我不知道如何解决这个问题。问题出在我的isset函数(在Cart.php中),因为我试图在那里回显一些东西,结果发现该函数没有运行。这是我在IF($This->;Items)中删除isset函数时弹出的error。我也尝试dd($Items),结果显示为空。为什么我的$Items没有包含一组$Items?你们知道为什么和如何解决这个问题吗?

p/s:在上一篇文章中,我删除了$oldCart,并将其替换为$Cart,我希望$Cart会覆盖它自己。 这些都是与我的购物车相关的代码。

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

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

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

解决方案

我认为您需要在Cart类中重新定义您的Add函数,如下所示,有一些语法错误(删除$Sign Form$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;
    }

相关文章