Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Live-Search in the Table

Premium
7 min read

In this lesson, let's add a search by name and category above the table. And here you will see where Livewire shines. After changing any of the inputs, the table will be refreshed without any JS code line.

live search table


Add Search Inputs

First, we need to add inputs above the table. But before that, let's quickly add categories to the products.

database/migrations/xxxx_create_categories_table.php:

Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});

database/migrations/xxxx_add_category_id_to_products_table.php:

Schema::table('products', function (Blueprint $table) {
$table->foreignId('category_id')->after('id');
});

app/Models/Product.php:

use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class Product extends Model
{
// ...
 
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
}

database/factories/CategoryFactory.php:

class CategoryFactory extends Factory
{
public function definition(): array
{
return [
'name' => $this->faker->words(asText: true),
];
}
}

database/factories/ProductFactory.php:

use App\Models\Category;
 
class ProductFactory extends Factory
{
public function definition(): array
{
$categories = collect(Category::pluck('id'));
 
return [
'category_id' => $categories->random(),
'name' => $this->faker->name(),
'description' => $this->faker->text(50),
];
}
}

database/seeders/DatabaseSeeder.php:

class DatabaseSeeder extends Seeder
{
public function run(): void
{
Category::factory(10)->create();
Product::factory(50)->create();
}
}

We must eager load categories in the Products Livewire component to avoid the N+1 issue. Also, I will refactor it into a separate variable for...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

N
Nurbek ✓ Link copied!

but i switch other page and return its don't work(filter) i wrote function updating

M
Modestas ✓ Link copied!

Can you expand on what stops working?

If you are talking about the filter being gone - that's expected as it's not saved anywhere.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.