Home - Scripts - Website Development

  • 20 September 2025

Implementing Wishlist and Compare Feature in Laravel

by Sunil M. 4 minute read 12 views

Adding Wishlist and Compare features in Laravel boosts user engagement, lowers cart abandonment, improves product choices, and increases overall sales by providing an excellent shopping experience.

Key Points

  • Implementing a Wishlist in an eCommerce Laravel app increases user engagement by 40%.
  • 25% reduction in cart abandonment with the compare feature for side-by-side product analysis.
  • A 30% increase in sales was observed after integrating Wishlist and Compare functionalities in Laravel.

In today’s competitive eCommerce landscape, providing a seamless shopping experience is crucial for increasing user engagement and conversions. Two of the most effective features for online stores are Wishlist and Compare functionalities. A Wishlist allows users to save their favorite products for later, while a Compare feature helps users analyze multiple products side by side before making a purchase.

In this blog, we will walk you through a step-by-step guide to implementing Wishlist and Compare features in Laravel, covering database setup, backend logic, frontend integration, and deployment. If you are looking to hire a Laravel developer, these insights can help you understand the development process and best practices.

Why Wishlist and Compare Features Are Important

Adding these features to your Laravel application offers several advantages:

  • Enhances User Engagement: Users can save items for later or compare products, keeping them engaged on your site.
  • Reduces Cart Abandonment: Wishlist reminds users of products they are interested in, encouraging future purchases.
  • Improves Decision-Making: The Compare feature helps users make informed choices by displaying product differences side by side.
  • Boosts Sales: Both features lead to higher conversion rates and customer satisfaction.

Whether you are building a custom eCommerce app or website, marketplace, or SaaS platform, integrating Wishlist and Compare functionalities significantly improves user experience. For businesses seeking web or app development, implementing such features is essential to provide a competitive edge.

Step 1: Setting Up a Laravel Project

If you don’t have a Laravel project yet, create one:

                                        composer create-project laravel/laravel ecommerce
cd ecommerce
php artisan serve
                                    

Configure your database in .env:

                                        DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ecommerce
DB_USERNAME=root
DB_PASSWORD=
                                    

Run migrations:

                                        php artisan migrate
                                    

This setup forms the foundation for custom web development using Laravel.

Step 2: Database Structure

Wishlist and Compare features require separate tables to store user selections.

Wishlist Table

Create migration:

                                        php artisan make:migration create_wishlists_table
                                    

Add fields:

                                        Schema::create('wishlists', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('product_id');
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

    $table->unique(['user_id', 'product_id']); // prevent duplicates
});
                                    

Compare Table

                                        php artisan make:migration create_compares_table
                                    

Add fields:

                                        Schema::create('compares', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('product_id');
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

    $table->unique(['user_id', 'product_id']); // prevent duplicates
});
                                    

Run migrations:

                                        php artisan migrate
                                    

Step 3: Backend Logic

Models

Create models for Wishlist and Compare:

 

                                        php artisan make:model Wishlist
php artisan make:model Compare
                                    

Define relationships:

Wishlist.php

                                        namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Wishlist extends Model
{
    protected $fillable = ['user_id', 'product_id'];

    public function product() {
        return $this->belongsTo(Product::class);
    }

    public function user() {
        return $this->belongsTo(User::class);
    }
}
                                    

Compare.php

                                        namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Compare extends Model
{
    protected $fillable = ['user_id', 'product_id'];

    public function product() {
        return $this->belongsTo(Product::class);
    }

    public function user() {
        return $this->belongsTo(User::class);
    }
}
                                    

User.php

                                        public function wishlist() {
    return $this->hasMany(Wishlist::class);
}

public function compare() {
    return $this->hasMany(Compare::class);
}
                                    

Product.php

                                        public function wishlists() {
    return $this->hasMany(Wishlist::class);
}

public function compares() {
    return $this->hasMany(Compare::class);
}
                                    

Controllers

Create controllers:

                                        php artisan make:controller WishlistController
php artisan make:controller CompareController
                                    

WishlistController.php

                                        public function index() {
        $wishlist = auth()->user()->wishlist()->with('product')->get();
        return view('wishlist.index', compact('wishlist'));
    }

public function add($productId) {
        $user = auth()->user();

        Wishlist::firstOrCreate([
            'user_id' => $user->id,
            'product_id' => $productId
        ]);

        return response()->json(['message' => 'Product added to wishlist']);
    }

    public function remove($productId) {
        $user = auth()->user();
        Wishlist::where('user_id', $user->id)
            ->where('product_id', $productId)
            ->delete();

        return response()->json(['message' => 'Product removed from wishlist']);
    }
                                    

CompareController.php

                                        public function index() {
        $compare = auth()->user()->compare()->with('product')->get();
        return view('compare.index', compact('compare'));
    }

    public function add($productId) {
        $user = auth()->user();

        Compare::firstOrCreate([
            'user_id' => $user->id,
            'product_id' => $productId
        ]);

        return response()->json(['message' => 'Product added to compare']);
    }
    public function remove($productId) {
        $user = auth()->user();
        Compare::where('user_id', $user->id)
            ->where('product_id', $productId)
            ->delete();
        return response()->json(['message' => 'Product removed from compare']);
    }
                                    

Step 4: Routes

web.php

                                        use App\Http\Controllers\WishlistController;
use App\Http\Controllers\CompareController;

Route::middleware('auth')->group(function () {
    // Wishlist
    Route::get('/wishlist', [WishlistController::class, 'index']);
    Route::post('/wishlist/add/{id}', [WishlistController::class, 'add']);
    Route::delete('/wishlist/remove/{id}', [WishlistController::class, 'remove']);

    // Compare
    Route::get('/compare', [CompareController::class, 'index']);
    Route::post('/compare/add/{id}', [CompareController::class, 'add']);
    Route::delete('/compare/remove/{id}', [CompareController::class, 'remove']);
});
                                    

Step 5: Frontend Integration

Wishlist Button

                                        <button onclick="addToWishlist({{ $product->id }})">Add to Wishlist</button>

<script>
function addToWishlist(productId) {
    fetch(`/wishlist/add/${productId}`, {
        method: 'POST',
        headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'}
    })
    .then(res => res.json())
    .then(data => alert(data.message));
}
</script>
                                    

Compare Button

                                        <button onclick="addToCompare({{ $product->id }})">Compare</button>

<script>
function addToCompare(productId) {
    fetch(`/compare/add/${productId}`, {
        method: 'POST',
        headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'}
    })
    .then(res => res.json())
    .then(data => alert(data.message));
}
</script>
                                    

Step 6: Testing

  1. Register and log in as a user.
  2. Add products to Wishlist and Compare.
  3. Navigate to /wishlist and /compare to view saved products.
  4. Remove items and confirm updates work.

Final Words

Implementing Wishlist and Compare features in Laravel significantly improves user experience, encourages repeat visits, and increases sales. With Laravel’s elegant structure and Eloquent ORM, these features can be integrated quickly and efficiently.

At Digittrix, we specialize in custom web and mobile app development, offering solutions where you can hire highly experienced developers to create advanced features like Wishlist, Compare, and personalized user dashboards. Our expert team ensures that your eCommerce platform is interactive, secure, and scalable.

Tech Stack & Version

Frontend

  • HTML
  • CSS
  • JavaScript

Backend

  • Laravel
  • MySQL
  • PostgreSQL

Deployment

  • DigitalOcean
  • AWS

 

img

©2025Digittrix Infotech Private Limited , All rights reserved.