我正在尝试激活购物车(laravel 项目)上的删除按钮。实现函数 destroy 后,我遇到了错误Call to undefined method App\Cart::delete()。根据我的观点,我也尝试{{ route('cart.destroy', ['id' => $product['item']['id']]) }}过我的观点,但仍然是一样的。似乎没问题,但我不知道该怎么办,我对此很困惑。购物车:php<?phpnamespace 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]); }
添加回答
举报
0/150
提交
取消