0

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],
]

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.