Reputation: 1054
I have a MongoDB collection organized like the following document:
{ "_id" : ObjectId("55f699bb1638cf0f4ba139f1"), "code" : 169, "categories" : "Consulenti del lavoro", "listing" : "B", "macrocategory" : "Consulenti" }
I need to get an array of the macrocategory field, but it's assigned to multiple documents and I need only one occurence of each of them. I tried using distinct in my Eloquent query, but I can't use it as I don't know which is the return value.
My query now, which returns the macrocategories multiple times:
$macrocategories = Category::orderBy('macrocategory','asc')->get();
Thanks!
Upvotes: 7
Views: 29504
Reputation: 1959
You suppose to use distinct when you want an entire row to be unique,when you want a specific column to be unique you should use GROUP BY
I guess the distinct would be the same as yours order by
coulmn -
$macrocategories = Category::orderBy('macrocategory','asc')->groupBy('macrocategory')->get();
BTW if you have columns that you don't need you should use select
and fetch only the columns you do.
Upvotes: 15