In this article, I will show you how to use group by in Laravel. Before we go straight to example, we should know what of the purpose of using group by function in Laravel. GroupBy is a function to group result base to columns with aggregated function.
GroupBy()
is usually to aggregated many functions such as MAX(), MIN(), SUM(), COUNT(), AVG()
to group result in one or more result. For example I have many users mix with male and female and I want to know how many user are female and male. In this case we will use COUNT()
and GroupBy()
function to count reference by grouping the results.
We will show various examples with eloquent in Laravel and raw SQL for who familar with SQL Statement. So, let's see how to use group by in laravel 6, laravel 7, and laravel 8 or laravel 9 with where clause.
SELECT column_names
FROM table_name
WHERE condition
GROUP BY column_names
To count each gender existing in table Student
DB::table('Student')
->select(DB::raw('gender'), DB::raw('count(gender) as number'))
->groupBy('gender')
->get();
Sample Output:
Before
name | gender |
---|---|
Sok | Male |
Sao | Male |
Bopha | Female |
Lina | Female |
Da | Male |
After
gender | number |
---|---|
Male | 3 |
Female | 2 |
In case we want to filter out what we have grouping, having clause work similar as where clause. Let's me explain a little bit where
vs having
clause. Basically, where
can be use with group by
as well as having
. where
is use for filter out record before work on grouping that's why in raw SQL you will see where
clause is written before group by
. In contrast, having clause will filter after grouping is ready and can work with aggreated data. I hope you will understand.
DB::table('Mark')
->select(DB:raw('student_id'),DB:raw('SUM(score) as total'))
->groupBy('student_id')
->having('total', '>', 60)
->get();
Sample Output:
Before
student_id | name | score |
100 | Lin | 60 |
100 | Lin | 35 |
101 | Mey | 30 |
101 | Mey | 30 |
102 | Lay | 40 |
102 | Lay | 45 |
After
student_id | total |
100 | 95 |
102 | 85 |
Example how we use where
combine with having
clause with grouping result.
DB::table('Mark')
->select(DB:raw('student_id'),DB:raw('SUM(score) as total'))
->where('score','>',35)
->groupBy('student_id')
->having('total', '>', 60)
->get();
Sample Output:
Before
student_id | name | score |
100 | Lin | 60 |
100 | Lin | 35 |
101 | Mey | 30 |
101 | Mey | 30 |
102 | Lay | 40 |
102 | Lay | 45 |
After
student_id | total |
102 | 85 |
The query will look over the records which have score greater than 35, so the result would be 3 records then we sum the score in remain ing records, thus the result is only one record display.
You might also like...
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him