I am trying to remove an item from the cart
this is my view cart in blade
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Product</th>
<th scope="col">Price</th>
<th scope="col">Qty</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<?php $total = 0; ?>
@foreach ($cart as $item)
<?php
$id=$item['id'];
/* dd($id); */
$total += $item['price'] * $item['qty']; ?>
<tr>
<td>{{ $item['name'] }}</td>
<td>${{ $item['price'] }}</td>
<td>{{ $item['qty'] }}</td>
<td><a href="{{route('cart.cartDelete', $id)}}">Cancella</a></td>
</tr>
@endforeach
</tbody>
</table>
this is my controller
class CartEcommerceController extends Controller
{
public function index()
{
$cart = session()->get('cart');
if ($cart == null)
$cart = [];
/* dd($cart); */
return view('cart.cart')->with('cart', $cart);
}
public function deleteItemCart($id){
$cart = Session()->get('cart');
$itemDelete=$cart[$id]['id'];
dd($itemDelete);
unset($cart[$id]);
session()->put('cart', $cart);
if (count($cart)==0) {
session()->forget('cart');
}
return redirect()->back();
}
}
when i click on delete i can pass the id but it does not enter the controller
and it returns me a page with 404 error page not found
this is my route
Route::post('cart/delete/{$id}', [CartEcommerceController::class,'deleteItemCart'])->name('cart.cartDelete');
what am i doing wrong?
My Answer:
@foreach($cartItems as $item)
{{ $item->name }}
{{ $item->price }}
@endforeach
In this code snippet, we are iterating over the items in the cart and displaying their name and price. We also have a form for each item with a "Remove" button. When the button is clicked, it will send a DELETE request to the route 'cart.remove' with the item's ID as a parameter. This route should be set up in your routes file to handle the removal of items from the cart.
Rate this post
4 of 5 based on 2173 votesComments