Home > front end >  How to filter Products through attributes (color, size etc.) in Laravel
How to filter Products through attributes (color, size etc.) in Laravel

Time:01-13

I am new to Laravel and want to filter out specific products.

I have two tables in my Database, the first one is table products and the second one is table attributes.

Products Table

 Schema::create('products', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->BigInteger('category_id')->unsigned()->nullable();
  $table->string('name');
  $table->string('code');
  $table->integer('status')->default(1);
  $table->integer('featured')->default(1);
  $table->string('image');
  $table->longText('short_description');
  $table->longText('long_description');
  $table->timestamps();
})

Product Attributes Table

Schema::create('product_attributes', function (Blueprint $table) {. 
  $table->bigIncrements('id');
  $table->unsignedBigInteger('product_id');
  $table->string('sku');
  $table->string('size'); 
  $table->string('color');
  $table->string('price');
  $table->string('stock');
  $table->timestamps();
})

Relationship

As i have multiple attributes of single product

class Product extends Model
{
    use HasFactory;

    public function attributes()
    {
         return $this->hasmany('App\Models\ProductAttributes', 'product_id');
    }
}

MY Blade file

<form action="{{url('/product/filter')}}" method="post">
 @csrf
<input type="hidden"value="{{$slug}}"name="slug">

<div
  >
   <input name="color" onchange="javascript:this.form.submit();" type="radio"  id="black" value="black">   <label  for="black">Black</label>
</div>

</form>

I have a function in my controller

public function shop()
{

$filter_products = Product::with('attributes')->where(['category_id' => $category->id, 'color' => $request->color]);

    return view('frontend.shop', compact('filter_products'));
}

After applying this function i got no result

Please guide me how can I filter products at my frontend shop page according to specific size or color. and what code will be in shop function.

please reply i will be very thankful to you

CodePudding user response:

You need filter by the relationship, take a look in the documentation

https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence

By example Using WhereHas

$filter_products = Product::with('attributes')
   ->where(['category_id' => $category->id])
   ->whereHas('attributes',function($query) use($request){
      $query->where(['color' => $request->color]);
   });

If in the with there is no where applied this will return all the attributes

You can use the same filter applied in whereHas to with to prevent this behaviour

$filter_products = Product::with(['attributes'=>function($query) use($request){
        $query->where(['color' => $request->color]);
    }])
    ->where(['category_id' => $category->id])
    ->whereHas('attributes',function($query) use($request){
        $query->where(['color' => $request->color]);
    });
   
  • Related