Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andreigirnet96's avatar

Still haven't solved it, trying to extract Products with 3 common colors(products having the same color but not less than three)

Still haven't solved it, trying to extract Products with 3 common colors(products having the same color but not less than three) Can I use somehow Eloquent or Laravel queries to do this task, Sorry guys I just can't find the solution anywhere in the internet. Thanks for advance for any contribution I will really appreciate. I will try to provide as many info as I can: So I have many to many relationship between Products and Colors tables.

class Product extends Model
{
    use HasFactory;
    protected $fillable = ['name'];
    public function colors(){
        return $this->belongsToMany(Color::class,'colors_products');
    }
}
class Color extends Model
{
    use HasFactory;
    protected $fillable = ['name'];

    public function products(){
        return $this->belongsToMany(Product::class,'colors_products');
    }
}
class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function index()
    {
        $products = Product::all();
        $colors = Color::all();
        $selected_products = Product::has('colors', '>=', 3)->get();

        return view('welcome', compact('products','colors','selected_products'));
    }
0 likes
9 replies
MohamedTammam's avatar

Use has: https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence

An example from the docs

// Retrieve all posts that have three or more comments...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
}, '>=', 10)->get();

An example related to your question

That will retrieve products that have at least 3 colors.

$products = Product::whereHas('colors', function($q) {
	$q->where('color', 'your_color');	// And you can keep chaining your query
}, '>=', 3)->get();
1 like
Tray2's avatar

So if I understand you correctly you have something like.

product 1 |red|green|blue|black
product 2 |red|green|white|pink
product 3 |red|green|purple|blue

and you want to get product 1 and three?

Not knowing your table structure it's hard to give you a nudge in the right direction.

However using Eloquent, the Query builder or even SQL for this is something that I don't think is possible since it's a bit more logic needed than what exists in SQL.

The only way I can see doing this is some very nasty if, else if structures.

Why do you need to do this?

1 like
andreigirnet96's avatar

@Tray2 It was a interview question that I couldn't solve, I just wanted to see how it could be solved

webrobert's avatar

@andy12441

This works. there is probably a sexier way. in real life, you'd build a filter class that looped to make this...

Product::with('colors')
  ->whereHas('colors', function($q) {
      $q->where('colors.name', 'red');
  })
  ->whereHas('colors', function($q) {
      $q->where('colors.name', 'yellow');
  })
  ->whereHas('colors', function($q) {
      $q->where('colors.name', 'blue');
  })
  ->get();

Returns all products that have these three colors.

1 like
andreigirnet96's avatar

Yes, seems like this is the only solution, thank you, in my task was specified that it should be a sql query, so maybe the task is not fromulated very correct

Please or to participate in this conversation.