猿问

Laravel 错误:调用未定义的方法 App\Cart::delete()

我正在尝试激活购物车(laravel 项目)上的删除按钮。实现函数 destroy 后,我遇到了错误Call to undefined method App\Cart::delete()。根据我的观点,我也尝试{{ route('cart.destroy', ['id' => $product['item']['id']]) }}过我的观点,但仍然是一样的。似乎没问题,但我不知道该怎么办,我对此很困惑。

购物车:php

<?php


namespace App;


class Cart 

{

   public $items = null;

   public $totalQty = 0;

   public $totalPrice = 0;


   public function __construct($oldCart)

   {

     if ($oldCart) {

        $this->items = $oldCart->items;

        $this->totalQty = $oldCart->totalQty;

        $this->totalPrice = $oldCart->totalPrice;

     }

   }


   public function add($item, $id)

   {

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


     if ($this->items ) {


            if (array_key_exists($id, $this->items)) {

                 $storedItem = $this->items[$id];

             }

         }

         $storedItem['qty']++;

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

         $storedItem['imagePath'] = $item->imagePath;

         $this->items[$id] = $storedItem;

         $this->totalQty++;

         $this->totalPrice += $item->price;

    }

}

购物车控制器.php:


class ProductController extends Controller


{

    /**

    *@return \Illuminate\Http\Response

    */

    public function index()

    {

        $products = Product::all();

        return view('home', ['products'=> $products]);

    }


    public function getAddToCart(Request $request, $id) 

    {

        $product = Product::find($id);

        $oldCart = Session::has('cart') ? Session::get('cart') : null;

        $cart = new Cart($oldCart);

        $cart->add($product, $product->id);

        

        $request->session()->put('cart', $cart);

        return redirect()->route('home');

    }


    public function getCart()

    {

        if (!Session::has('cart')) {

            return view('cart');

        }

        $oldCart = Session::get('cart');


        $cart = new Cart($oldCart); 

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

    }


慕容708150
浏览 168回答 1
1回答

PIPIONE

您的 Cart 类中没有删除功能。您需要创建一个。
随时随地看视频慕课网APP
我要回答