Laravel 11 E-Commerce - Remove Product from Wishlist
Welcome back to the Laravel E-Commerce Tutorial. In this video, we will learn about removing products from a wishlist.
Step 1: Create a Function in the Wishlist Controller
First, let's create a function in the Wishlist Controller:
public function remove_item_from_wishlist($rowId)
{
Cart::instance('wishlist')->remove($rowId);
return redirect()->back();
}
Step 2: Create a Route for the Function
Next, create a route for this function:
Route::delete('/wishlist/remove/{rowId}',[WishlistController::class,'remove_item_from_wishlist'])->name('wishlist.remove');
Step 3: Add a Form to the Wishlist View
Now, go to the wishlist.blade.php file and create a form:
Now that we've completed the setup, let's check it out! Refresh the page and remove a product from the wishlist by clicking on the icon. You should see that the product has been removed.
Step 4: Create a Function to Clear All Items
Now, let's make the "Clear all items" link work. Go to the Wishlist Controller and create a new function:
public function empty_wishlist()
{
Cart::instance('wishlist')->destroy();
return redirect()->back();
}
Step 5: Create a Route for the Clear All Items Function
Next, go to the web.php file and create a route:
Route::delete('/wishlist/clear',[WishlistController::class,'empty_wishlist'])->name('wishlist.empty');
Step 6: Add a Form to the Wishlist View for Clearing All Items
Now, go to the wishlist.blade.php file and add a form:
<td>
<div class="del-action">
<form method="POST" action="{{route('wishlist.remove',['rowId'=>$wishlistItem->rowId])}}">
@csrf
@method("DELETE")
<button type="submit" class="remove-cart btn btn-sm btn-danger">Remove</button>
</form>
</div>
</td>
Final Step: Check the Clear All Items Feature
Now that we've completed the setup, let's check it out! Refresh the page and clear all items from the wishlist by clicking on the link. You should see that all items have been removed.
In this way, you can remove products from a wishlist.