0
0
Laravelframework~10 mins

Aggregates (count, sum, avg) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Aggregates (count, sum, avg)
Start Query Builder
Select Aggregate Function
Apply Conditions (optional)
Execute Query
Return Aggregate Result
End
This flow shows how Laravel builds and runs an aggregate query like count, sum, or avg, then returns the result.
Execution Sample
Laravel
<?php
$count = DB::table('orders')->count();
$sum = DB::table('orders')->sum('amount');
$avg = DB::table('orders')->avg('amount');
This code counts all orders, sums the 'amount' column, and calculates the average of 'amount' in the 'orders' table.
Execution Table
StepQuery Builder ActionSQL GeneratedResult ValueExplanation
1Start query on 'orders' tableSELECT COUNT(*) FROM ordersnullPrepare to count all rows in 'orders'
2Execute count()SELECT COUNT(*) FROM orders5Returns total number of rows, here 5 orders
3Start query on 'orders' tableSELECT SUM(amount) FROM ordersnullPrepare to sum 'amount' column
4Execute sum('amount')SELECT SUM(amount) FROM orders250Returns sum of 'amount' values, here 250
5Start query on 'orders' tableSELECT AVG(amount) FROM ordersnullPrepare to average 'amount' column
6Execute avg('amount')SELECT AVG(amount) FROM orders50Returns average of 'amount' values, here 50
7EndN/AN/AAll aggregate queries completed
💡 All aggregate queries executed and results returned
Variable Tracker
VariableStartAfter count()After sum()After avg()
$countnull555
$sumnullnull250250
$avgnullnullnull50
Key Moments - 2 Insights
Why does count() not need a column name but sum() and avg() do?
count() counts rows, so it works without a column. sum() and avg() need a specific column to calculate totals or averages, as shown in execution_table rows 1-2 vs 3-6.
What happens if the table is empty when calling these aggregates?
count() returns 0 because no rows exist. sum() and avg() return null because there are no values to add or average, similar to the initial 'null' results in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $sum after step 4?
A5
B250
C50
Dnull
💡 Hint
Check the 'Result Value' column at step 4 for sum('amount')
At which step does the avg('amount') query execute?
AStep 6
BStep 4
CStep 3
DStep 2
💡 Hint
Look for 'Execute avg('amount')' in the 'Query Builder Action' column
If the 'orders' table had zero rows, what would $count be after step 2?
A1
Bnull
C0
DError
💡 Hint
Refer to key_moments about empty table behavior and execution_table step 2
Concept Snapshot
Laravel aggregates use query builder methods:
- count() counts rows, no column needed
- sum('column') adds values in a column
- avg('column') calculates average of a column
They generate SQL like SELECT COUNT(*), SUM(col), AVG(col)
Results are returned directly as numbers
Use for quick summary data from tables
Full Transcript
This visual trace shows how Laravel's query builder runs aggregate functions count, sum, and avg on a database table. First, it starts a query on the 'orders' table. Then it builds SQL commands like SELECT COUNT(*), SELECT SUM(amount), and SELECT AVG(amount). Each query runs and returns a number: count returns the number of rows, sum adds all values in the 'amount' column, and avg calculates their average. Variables $count, $sum, and $avg hold these results. Beginners often wonder why count() needs no column but sum() and avg() do; this is because count counts rows, while sum and avg need a specific column to operate on. If the table is empty, count returns 0, but sum and avg return null. This trace helps learners see each step and variable change clearly.