Ad

How Can I Make Query For Relationship?

- 1 answer

I have a product table and a color table. And I have the following relation for them:

public function colors() {
   return $this->belongsToMany('App\Color','product_colors');
}

I want to find out what product have more than 3 colors. How can I do this?

Ad

Answer

You can try simply with this :

$products = Product::has('colors', '>', 3)->get()
Ad
source: stackoverflow.com
Ad