Run the following Artisan command to generate the Product model and migration:
php artisan make:model Product -m
Open the generated migration file and define the structure of the products
table:
id();
$table->string('name');
$table->string('category');
$table->decimal('price', 8, 2);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
Execute the migration to create the products
table in your database:
php artisan migrate
Create a seeder to populate the products
table with sample data:
php artisan make:seeder ProductSeeder
Open the generated seeder file in database/seeders/ProductSeeder.php
and add the following:
'Smartphone', 'category' => 'electronics', 'price' => 699.99 ]);
Product::create([ 'name' => 'Laptop', 'category' => 'electronics', 'price' => 999.99 ]);
Product::create([ 'name' => 'T-Shirt', 'category' => 'fashion', 'price' => 19.99 ]);
Product::create([ 'name' => 'Jeans', 'category' => 'fashion', 'price' => 49.99 ]);
Product::create([ 'name' => 'Headphones', 'category' => 'electronics', 'price' => 199.99 ]);
}
}
Run the seeder to populate the database:
php artisan db:seed --class=ProductSeeder
We’ll begin by creating a new Livewire component for the search and filtering functionality:
php artisan make:livewire SearchFilter
This will generate:
app/Livewire/SearchFilter.php
– The PHP class for the component logic.
resources/views/livewire/search-filter.blade.php
– The Blade file for the search UI.
Open SearchFilter.php
and update it with the following logic:
when($this->search, function ($query) {
$query->where('name', 'like', '%' . $this->search . '%');
})
->when($this->category, function ($query) {
$query->where('category', $this->category);
})
->when($this->priceRange, function ($query) {
[$min, $max] = explode('-', $this->priceRange);
$query->whereBetween('price', [(int)$min, (int)$max]);
})
->get();
return view('livewire.search-filter', [
'products' => $products,
]);
}
}
Dynamic Filters: The when()
method dynamically applies filters based on user input.
Search Field: Filters results based on product names.
Category and Price Filters: Allows filtering by category and price range.
Open search-filter.blade.php
and design the search and filter interface:
@if ($products->isEmpty())
No products found.
@else
@foreach ($products as $product)
-
{{ $product->name }}
Category: {{ $product->category }}
Price: ${{ $product->price }}
@endforeach
@endif
Real-time search allows users to see the results instantly as they type in the search box. This is achieved through the wire:model
** binding**, which keeps the backend and frontend properties in sync without page reloads.
If a user types “Phone” in the search box, Livewire will filter and display only the products matching “Phone” instantly.
With the above setup, your search and filtering component will:
Allow users to search for products by name in real time.
Filter results by category and price range dynamically.
Display updated results instantly without reloading the page
Real-time search using wire:model
.
Dynamic filtering with multiple criteria.
Query building using when()
.
Responsive and styled UI for search and filter components.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.