Support the ongoing development of Laravel.io →
Database Eloquent

I have a simple filtering on my catalog. It allows me to choose the product based on its attributes and its values. For instance we have a set of beads:

  1. Stone bead it has a size of 12mm, round shape and material of stone

  2. Glass/Wooden/Plastic bead it has a size of 26mm, square shape and material of glass, wood and plastic

The problem I'm facing is duplicates when I choose glass, wood and plastic materials in the filter, since one product has it all.

One product shows three times.

Here is the code for getting products: http://laravel.io/bin/WzRym

Which generates the following query:

select * from `products` inner join `products_attributes_values` as `pav0` on `pav0`.`product_id` = `products`.`id` where `category_id` in ('6') and `availability` = 'in_stock' and `pav0`.`attribute_id` = '2' and `pav0`.`value_id` in ('39', '93', '96') order by `id` desc limit 48 offset 0

How could I omit the duplicates?

Last updated 3 years ago.
0

Try adding

group by `products`.`id`
Last updated 3 years ago.
0

Thanks! That's fixed it

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.