For getting every new tutorial link Please join our telegram group

Part 13 laravel aggregate functions with example like avg(), sum(), max() and count()

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.

  1. avg()
  2. sum()
  3. min()
  4. max()
  5. 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.

php laravel developer anmol sharma

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