我正试图从购物车中删除产品,但未删除产品,修复它的任何建议,当我单击“提交”按钮时,提示“ 404 |未找到
控制器:
public function deleteCartProduct(Product $product)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->delProduct($product);
Session::put('cart', $cart);
return redirect()->route('product.cart')->with('flash_message_success', 'Product product has been removed from Cart');
}
模型
public function deleteProduct($product)
{
if ($this->contents) {
if (array_key_exists($product->product_slug, $this->contents)) {
$delProduct = $this->contents[$product->slug];
$this->totalQty -= $delProduct['qty'];
$this->totalPrice -= $delProduct['price'];
array_forget($this->contents, $product->slug);
}
}
}
刀片文件
@foreach($contents as $slug => $cartItem)
<form action="{{ route('deleteCartProduct', $product) }}" method="POST">
@csrf
<tr class="table-row">
<td class="column-1">
<div class="cart-img-product b-rad-4 o-f-hidden">
<img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $cartItem['product']->product_image }}" alt="IMG-PRODUCT">
</div>
</td>
<td class="column-2">{{ $cartItem['product']->product_name }}</td>
<td class="column-3">${{ $cartItem['product']->product_price }}</td>
<td class="column-4">
<div class="flex-w bo5 of-hidden w-size17">
<button class="btn-num-product-down color1 flex-c-m size7 bg8 eff2">
<i class="fs-12 fa fa-minus" aria-hidden="true"></i>
</button>
<input class="size8 m-text18 t-center num-product" type="number" name="num-product1" value="{{ $cartItem['qty'] }}">
<button class="btn-num-product-up color1 flex-c-m size7 bg8 eff2">
<i class="fs-12 fa fa-plus" aria-hidden="true"></i>
</button>
</div>
</td>
<td class="column-5">${{ $cartItem['price'] }}</td>
<td class="column-5">
<input type="submit" class="btn btn-danger value="Remove Product">
</td>
</tr>
</form>
@endforeach
路线:
Route::get('/cart/delete-product/{id}','ProductController@deleteCartProduct')->name('deleteCartProduct');
答案 0 :(得分:0)
您的路线应为Route::delete
,而不是Route::get
,然后在表单中也添加以下内容:
@method('delete')
我在按钮上看到您的错误:
<input type="submit" class="btn btn-danger value="Remove Product">
对此进行更改:
<input type="submit" class="btn btn-danger" value="Remove Product">
缺少报价。
编辑
您的路线应为:
Route::delete('/cart/delete-product/{id}','Admin\ProductController@deleteCartProduct')->name('deleteCartProduct');
答案 1 :(得分:0)
您需要按如下所示更改控制器中的代码。
public function deleteCartProduct(Product $product)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->deleteProduct($product);
Session::put('cart', $cart);
return redirect()->route('product.cart')->with('flash_message_success', 'Product product has been removed from Cart');
}
答案 2 :(得分:0)
您在刀片中将其定义为method =“ POST”。和“获取”路线。因此,您需要将动词更改为Route :: post