shahr's avatar
Level 10

laravel colors filters

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

0 likes
17 replies
jlrdw's avatar

You know how a modal works right? So what you want is similar but much more involved. It's a matter of keeping up with what to show and what to hide. And in back end use scopes or strategic if statements to requery and load the new data, but also the left panel of choices needs updating.

So bottom line it will take a little work with show or hide divisions and reloading data.

See if any shopping carts have this on Github, and you can use the code as a guide (how to).

MichalOravec's avatar

Text copied from this thread.

Oxbir, why do you have to copy a text from somebody else?

aurelianspodarec's avatar

@jlrdw I'm not that person but maybe Michal looks at the level and check every level 4 :D My guess that is :D

aurelianspodarec's avatar

His nickname also looks pretty similar

Another thing he might have done is perhaps go to one of the old posts and check his nickname there, and write a script to grab that nickname every few minutes and then alert him on screen with JS if it matches? :D

Snapey's avatar

@boton I thought you were leaving to go and sell office furniture ?

shahr's avatar
Level 10

😝😝😝😝😝😝I tried and solved it myself.😝😝😝😝😝😝

Snapey's avatar

@oxbir I don't believe you.

As you copied the question from someone else, where did you copy the answer from?

shahr's avatar
Level 10

@Snapey I copy my question because

I do not know what to write for you to understand.

I copied it from someone else Because I liked this question, I copied.

Please or to participate in this conversation.