Laravel 12 E-Commerce: Show All Wishlisted Products
This tutorial details creating the Wishlist page to display all products added to the wishlist cart instance, along with options to remove them or move them to the main shopping cart.
1. Update WishlistController for Index View
The index function in `WishlistController.php` is created to fetch all items from the wishlist instance of the shopping cart and pass them to the view.
`WishlistController.php` - `index` function
public function index()
{
// Fetch all items from the "wishlist" instance
$items = Cart::instance("wishlist")->content();
// Pass the items to the wishlist view
return view("wishlist", compact("items"));
}
Route in `web.php`
Define the route for accessing the wishlist page.
Route::get("/wishlist",[WishlistController::class,"index"])->name("wishlist.index");
2. Create `wishlist.blade.php` View
Create the view to iterate over the wishlist items and display product details and actions.
`wishlist.blade.php` Structure and Item Loop
<x-app-layout>
<!-- Breadcrumb and Section title here -->
<div class="wishlist-page-wrapper">
@if (Cart::instance("wishlist")->content()->count() > 0)
<div class="cart-table_wrapper">
<table class="table">
<!-- Table Header (Product, Price, Action) -->
<tbody>
@foreach (Cart::instance("wishlist")->content() as $wishlistItem)
<tr class="wishlist-item-{{ $wishlistItem->rowId }}">
<td class="product-thumb">
<!-- Product image display logic -->
</td>
<td class="product-name">
<a href="{{ route("shop.product.details", ["product_slug" => $wishlistItem->model->slug]) }}">{{ $wishlistItem->name }}</a>
</td>
<td class="product-price">${{ $wishlistItem->price }}</td>
<td class="product-action">
<div class="del-action">
<!-- Move to Cart Form -->
<form method="POST" action="{{ route("wishlist.move.to.cart", ["rowId" => $wishlistItem->rowId]) }}">
@csrf
<button type="submit" class="remove-cart btn btn-sm btn-warning">Move to Cart</button>
</form>
<!-- Remove From Wishlist Form -->
<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>
</tr>
@endforeach
</tbody>
</table>
</div>
@else
<!-- Empty Wishlist Message -->
<div class="row">
<div class="col-md-12">
<p>No item found in your wishlist</p><br>
<a href="{{ route("shop.index") }}" class="btn btn-info">Wishlist Now</a>
</div>
</div>
@endif
</div>
</x-app-layout>
3. New Routes for Wishlist Actions (Placeholder)
You will need to define routes and controller methods for removing items and moving them to the main cart. These are typically covered in subsequent parts.
Required New Routes (Assuming future implementation)
// Route to remove a product from the wishlist
Route::delete("/wishlist/remove/{rowId}",[WishlistController::class,"remove_from_wishlist"])->name("wishlist.remove");
// Route to move a product from the wishlist to the shopping cart
Route::post("/wishlist/move-to-cart/{rowId}",[WishlistController::class,"move_to_cart"])->name("wishlist.move.to.cart");
This completes the setup for displaying all wishlisted products on a dedicated page.
