Laravel 购物车。如何实现删除方法?
Laravel cart. How to implement delete method?
想在 laravel 中添加到购物车的删除方法。得到一个错误
函数 App\Http\Controllers\ProductController::delCartItem() 的参数太少,0 个通过,正好 1 个预期
我不通过argument.I明白了。我在控制器中的代码
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");
我可以在模板中使用
{{ $cart_item["id"] }}
它会显示 id
更改路由定义以接受将作为参数传递给 delCartItem
控制器方法的 id
参数:
Route::get("/del/{id}", "ProductController@delCartItem")->name("product.delCartItem");
不确定在你的情况下是否必须使用 $cart_item['id']
或 $cart_item['item']['id']
,但将 link 中的 route
辅助参数更改为数组:
<a href="{{ route('product.delCartItem', ['id' => $cart_item['id']]) }}">Del item</a>
想在 laravel 中添加到购物车的删除方法。得到一个错误
函数 App\Http\Controllers\ProductController::delCartItem() 的参数太少,0 个通过,正好 1 个预期
我不通过argument.I明白了。我在控制器中的代码
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");
我可以在模板中使用
{{ $cart_item["id"] }}
它会显示 id更改路由定义以接受将作为参数传递给 delCartItem
控制器方法的 id
参数:
Route::get("/del/{id}", "ProductController@delCartItem")->name("product.delCartItem");
不确定在你的情况下是否必须使用 $cart_item['id']
或 $cart_item['item']['id']
,但将 link 中的 route
辅助参数更改为数组:
<a href="{{ route('product.delCartItem', ['id' => $cart_item['id']]) }}">Del item</a>