I have a table that looks like the one below:
id | age | color |
---|---|---|
1 | 2 | blue |
3 | 3 | red |
4 | 5 | green |
5 | 5 | purple |
I want to get all the rows that have a unique age so something like the following would be returned:
id | age | color |
---|---|---|
1 | 2 | blue |
3 | 3 | red |
4 | 5 | green |
$baseQuery = myModel::query()
->select('id', 'age', 'color')
->distinct('age')
->get()
This seems to return everything, but the second my select only has age
column it removes the duplicates. The problem is i need the entire rows and not just age. Any ideas?
Edit: Another thing I tried was:
$baseQuery = myModel::query()
->select('id', 'age', 'color')
->groupBy('age')
->get()
This just threw a 1055 SQL error:
Syntax error or access violation: 1055 'table.color' isn't in GROUP BY
Thanks a bunch!
Edit I figured it out: Add the following before the query followed by the group by query above.
\DB::statement("SET SQL_MODE=''");
$baseQuery = myModel::query()
->select('id', 'age', 'color')
->groupBy('age')
->get()