In this tutorial we are going to learn about and Clear Product from the Cart.
So let see how can we delete and clear product from the cart.
For deleting cart product lets create a function inside the CartController
Go to the CartController and inside this file lets create a function for delete product from cart by rowId.



public function removeItem(Request $request)
{
$rowId = $request->rowId;
Cart::instance('cart')->remove($rowId);
return redirect()->route('cart.index');
}


Now lets create route for this function


Route::delete('/cart/remove', [CartController::class, 'removeCart'])->name('cart.remove');

Now here create a form

<form id="deleteFromCart" action="{{route('cart.remove')}}" method="post">
@csrf
@method('delete')
<input type="hidden" id="rowId_D" name="rowId" />
</form>


Now submit this form using a javascript function so inside the push directives add a javascript function as following


@push('scripts')
<script>
function removeItemFromCart(rowId)
{
$('#rowId_D').val(rowId);
$('#deleteFromCart').submit();
}
</script>
@endpush


Now call this removeFromCart function from remove link


<td>
<a href="javascript:void(0)" onclick="removeItemFromCart('{{$item->rowId}}')">
<i class="fas fa-times"></i>
</a>
</td>


Now its done lets check
So go to the browser and refresh the page
Now lets remove this from the cart so click on this link.
You can see product has been removed from cart

Now let's see how can clear the cart for that go to the CartController and here lets create one more function


public function clearCart()
{
Cart::instance('cart')->destroy();
return redirect()->route('cart.index');
}


Now create route for this function.
So go to the web.php file and create a new route here as following


Route::delete('/cart/clear', [CartController::class, 'clearCart'])->name('cart.clear');


now go to the Cart.blade.php file and create a form


<form id="clearCart" action="{{route('cart.clear')}}" method="post">
@csrf
@method('delete')
</form>


Now submit this form using javascript function so lets create a javascript function here

@push('scripts')
<script>
function clearCart()
{
$('#clearCart').submit();
}
</script>
@endpush


Now call this javascript function from the Clear all item anchor tag using onclick event.


<div class="col-sm-7 col-5 order-1">
<div class="left-side-button text-end d-flex d-block justify-content-end">
<a href="javascript:void(0)" onclick="clearCart()" class="text-decoration-underline theme-color d-block text-capitalize">clear all items</a>
</div>
</div>


So switch to the browser and lets add some product to the cart and
And now click on Clear all Items
Now you can see her all products have been removed from the cart
So in this way you can remove and empty the cart in laravel 10 ecommerce project.