Home > Back-end >  how can we use with method in laravel with where clause?
how can we use with method in laravel with where clause?

Time:12-03

i need only those products where variants size field is tiny. what i should do ? above quering giving me all products but with empty variants

$result=$categories->with([
                'products' => function ($product) {
                    return $product->with([
                        'variants' => function ($variants) {
                            return $variants->where('size','tiny');
                        }
                    ]);
                }
            ])->get();

CodePudding user response:

Change your code like this.

$result=$categories->with([
                'products' => function ($product) {
                    return $product->whereHas(
                        'variants', function ($variants) {
                            return $variants->where('size','tiny');
                        }
                    ]);
                }
            ])->get();
  • Related