Reputation: 13
Is it possible to group distinct table values using an array of columns - to group by each individual column. For example:
$columns = ['column1','column2','column3'];
$array = Model::select($columns)->distinct()->get()->groupBy($columns)->keys();
And I would like this to return (obviously my code isn't accurate and i'd like a way to do this without a for loop... I feel collections have the functionality to do this):
[
'column1' => [...distinct column1 values],
'column2' => [...distinct column2 values],
'column3' => [...distinct column3 values],
]
Upvotes: 0
Views: 298
Reputation: 1304
This should do the work. You had switched the places of get()
and groupBy()
$columns = ['column1','column2','column3'];
$array = Model::select($columns)->distinct()->groupBy($columns)->get()->keys();
Upvotes: 1