In this tutorial we read about laravel query builder all aggregate functions like avg, sum, min, max, count. Aggregate function use for calculation in numeric values if we want to calculate sum , average of numbers or calculate maximum and minimum salary of employees.
Laravel aggregate functions.
- avg()
- sum()
- min()
- max()
- count()
Laravel calculate average of numbers
How to calculate average value of some numbers than use avg() aggregate function. Let an example calculate average salary of employees.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class EmployeeController extends Controller{ public function index(){ $average_salary = DB::table('employees')->avg('salary'); dd($average_salary); } }
Find addition of numbers
How to calculate addition of some numbers than use sum() aggregate function it calculate addition of one table column of given rows according to condition. Let an example calculate sum of price of fruits category products.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ProductController extends Controller{ public function index(){ $price_sum = DB::table('products')>where('category', 'fruits')->sum('price'); dd($price_sum); } }
Laravel min() aggregate function
Laravel min() aggregate function use for calculate minimum or shortest value of numbers. Let an example find shortest price product in products table.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ProductController extends Controller{ public function index(){ $min_price = DB::table('products')->min('price'); dd($min_price); } }
Laravel max() aggregate function
Laravel max() aggregate function use for calculate maximun or largest value of numbers. Let an example find largest price product in products table.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ProductController extends Controller{ public function index(){ $max_price = DB::table('products')->max('price'); dd($max_price); } }
Laravel count() aggregate function
Laravel count() aggregate function use for getting number of rows that are match condition. Let and example find the number of employees that salary grater than 10000.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class EmployeeController extends Controller{ public function index(){ $count = DB::table('employees')->where('salary', '>', '10000')->count(); dd($count); } }
You read this tutorial on advanced web tuts. here we provide beginners to advanced tutorial.
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- laravel query builder introduction tutorial
- part 1 laravel query insert data
- part 2 laravel all type of where conditions in query builder
- part 3 laravel whereDate, whereYear and whereTime qyery method
- part 4 compare two db columns in laravel query builder
- part 6 laravel pluck method get single column value
- part 7 laravel select and addselect query in query builder
- part 5 laravel first and find queries
- part 8 laravel edit and update method query explaination
- Part 9 laravel query for update or insert data by one method
- part 10 laravel delete vs truncate vs drop method
- part 11 gropupby and orderby query tutorial
- Part 12 laravel union of two queries
- part 13 laravel all aggregate functions with example
- part 14 laravel query builder chunk method
- part 15 laravel exists() and doesntExist() method
- part 16 laravel paginate vs simplePaginate method
- part 17 laravel all join methods