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

andreigirnet96's avatar

Trying to get products with minimum of 3 similar colors

This code is returning products with minimum of three colors, how can I get the similar ones, any advice will help. Thanks to all

$sel_products = Product::has('colors', '>=', 3)->get();
0 likes
25 replies
Snapey's avatar

Similar in what way? The same number of letters in the name? like 'brown' and 'green' ?

andreigirnet96's avatar

@RutyCodes Hmm that is not going to work, if the lenght of the colors are similar, I want to return products which have 3 colors minimum and all the colors being the same

orest's avatar

Is there a name or title attribute in the colors table and you want to filter by that attribute ?

orest's avatar

@andrei.girnet96@gmail.com

If this is the case maybe you can do


Product::whereHas('colors', function($query){
return    $query->whereIn('name', ['red', 'blue'])->havingRaw('count(*) >= ?', [3])->groupBy('product_id');

})->get();

andreigirnet96's avatar

@orest I am getting this error Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Illuminate\Database\Eloquent\Builder given, called in C:\xampp\htdocs\test\vendor\laravel\framework\src\Illuminate\Http\Response.php on line 72

andreigirnet96's avatar

@orest It worked, but it is returning just one product, it should return two of them as just two products have 3 colors and they are the same

andreigirnet96's avatar

@orest

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');
}

}

public function index()
    {
        $products = Product::all();
        $colors = Color::all();
        $selected_products = Product::has('colors', '>=', 3)->get();
        return view('welcome', compact('products','colors','selected_products'));
    }
MichalOravec's avatar

Why do you need to get products which have a color with the same number of letters?

Explain for what it's good for?

andreigirnet96's avatar

@MichalOravec It is just a task I need to complete from school, I I have spent all my day serching how but I havent found the answer, there is not much in internet about this. I need products that have 3 colors and the colors are the same for example : Tshirt - blue, yellow, red Jeans- blue, yellow, red

andreigirnet96's avatar

Could you please at least tell me where to dig deeper, where to search, or what to learn

MichalOravec's avatar

Most likely this is what you need:

$colors = ['blue', 'yellow', 'red'];

$products = Product::whereHas('colors', function ($query) use ($colors) {
    $query->distinct()->whereIn('name', $colors);
}, '=', count($colors))->get();
Snapey's avatar

@michaloravec

the string length bit was me taking the mickey out of the requirement of "how can I get the similar ones"

I asked similar how? I was not expecting a response of 'yes'

MichalOravec's avatar

@Snapey Yeah I know, when I saw his response "yes", in my head was like: "why is it good for?!"

Sometimes it's very difficult to help other people with their issues.

Please or to participate in this conversation.