GroupBy Table Widget

Hi, anyone can help me with a table widget? I want to group by "bairros" and show it, but I get this error:
Embaixador::query()
->select('bairro', DB::raw('COUNT(*) as total'))
->groupBy('bairro')
->orderByDesc('total')
->limit(5)
Embaixador::query()
->select('bairro', DB::raw('COUNT(*) as total'))
->groupBy('bairro')
->orderByDesc('total')
->limit(5)
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'f3x_charbel.embaixadores.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

select `bairro`, COUNT(*) as total from `embaixadores` group by `bairro` order by `total` desc, `embaixadores`.`id` asc limit 11 offset 0
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'f3x_charbel.embaixadores.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

select `bairro`, COUNT(*) as total from `embaixadores` group by `bairro` order by `total` desc, `embaixadores`.`id` asc limit 11 offset 0
I think it's trying to get the id to sort, but dont have id column after groupBy
2 Replies
Jr.Pikong
Jr.Pikong4mo ago
try DB::raw('ANY_VALUE(COUNT(*) ) as total'),
👑 João
👑 João4mo ago
nothing...