A
A
Alexey Reut2021-09-11 19:28:53
Laravel
Alexey Reut, 2021-09-11 19:28:53

Laravel, multiple aggregates in one column?

Hello! There is a table with a color(string) field, how to get the number of each color in one query?
The result is like this:
red 20
green 45
black 23

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Nazarov, 2021-09-12
@vnaz

Dig mentioned in the comments
https://laravel.com/docs/8.x/queries#grouping
And pay attention to this:

->selectRaw('count(id) as number_of_orders, customer_id')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question