希望在laravel中将delete方法添加到购物车。得到一个错误
函数App\Http\Controllers\ProductController::delcarttitem()的参数太少,传递的参数为0,应为1
我不同意争论,我明白。我在控制器中的代码
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);
//Session::flush();
//dd($request->session()->get("cart"));
return redirect()->route('product.index');
}
public function getCart() {
//Session::flush();
if (!Session::has('cart')) {
return view('cart.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart.shopping-cart', ['cart' => $cart, 'products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
public function delCartItem(Request $request, $id){
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->del($product, $product->id);
$request->session()->put('cart', $cart);
//dd($request->session()->get("cart"));
return redirect()->route('product.index');
}
和模板中的代码
@foreach($cart->items as $cart_item)
<h4>Product</h4>
<?php
$a = ($cart_item["item"]["price"]*$cart_item["qty"]);
?>
<p> Name : {{ $cart_item['item']['name'] }}</p>
<p> Price : {{ $cart_item["item"]["price"]}} / Total: {{ $a }}</p>
<p> Qty : {{ $cart_item['qty'] }}</p>
<a href="{{ route('product.delCartItem', $cart_item['id']) }}">Del item</a>
@endforeach
@endif
路由不通过'$cart_item['id']'。
Route::get("/add-to-cart/{id}", "ProductController@getAddToCart")->name("product.addToCart");
Route::get("/shopping-cart", "ProductController@getCart")->name("product.shoppingCart");
Route::get("/del", "ProductController@delCartItem")->name("product.delCartItem");
我可以用
在模板中,它将显示id