I have seen many e-commerce sites in which users can filter products by clicking checkboxes. for example: let's say there are different brands of mobile: like Samsung, Nokia, Company, Company, etc. Different colors, sizes, and memory.
When the user checks the Samsung checkbox pages display only Samsung mobile when the user further checks the 8Gb memory checkbox, the page display Samsung mobile with 8Gb memory and so on.
I want to implement such a feature with jQuery/Ajax and Laravel. Can you give me an idea of how this is done?
I have four tables, colors and categories, and products, 'color_product'.
colors table
public function up()
{
Schema::create('colors', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->string('code');
$table->timestamps();
});
}
categories table
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->string('slug');
$table->bigInteger('parent_id');
$table->string('icon')->nullable();
$table->timestamps();
});
}
products table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->string('slug');
$table->integer('price');
$table->integer('inventory')->default(0);
$table->boolean('special')->default(0);
$table->text('body');
$table->text('description');
$table->text('properties');
$table->timestamps();
});
Schema::create('color_product', function (Blueprint $table) {
$table->foreignId('color_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->primary(['color_id' , 'product_id']);
});
Schema::create('category_product', function (Blueprint $table) {
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->primary(['category_id' , 'product_id']);
});
}
My blade
<div class="card mb-3">
<div class="card-body">
<label for="color_id" class="form-label">Color </label>
@foreach($colors as $color)
<p class="mb-1">
<label for="color_{{ $color->id }}" class="checkbox" data-bs-toggle="tooltip" data-bs-placement="top" title="{{ $color->name }}">
<input type="checkbox" id="color_{{ $color->id }}" name="color_id" value="{{ $color->id }}" onclick="colorCheckBox({{ $category->id }},this)">
<span class="checkmark" style="background-color: {{ $color->code }}"></span>
</label>
</p>
@endforeach
</div>
</div>
The rest I do not know what to write, that is, in control and Ajax. Write to me yourself.
For example.
I want to create a filter color like this
https://wpbingosite.com/wordpress/omeli/product-category/drinks/
This is the home page
https://wpbingosite.com/wordpress/omeli/home-6/
They are categories. When a user clicks on the category, open a category of products.
demo