For getting every new tutorial link Please join our telegram group

Part 12 laravel eloquent all aggregate methods like min, max, avg, count and sum

This laravel tutorial is about eloquent model aggregate functions. We will use eloquent aggregates for database table data count, sum, max, min and average value find.

Laravel eloquent model aggregate functions

  1. avg()
  2. sum()
  3. min()
  4. max()
  5. count()

Laravel eloquent avg() method

Calculate the average value of a column by the laravel eloquent use avg() method. let take an example of eloquent avg aggregate function. here we use a simple avg() method on the Student model and pass column name 'marks' it gives average marks value of all students

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller{
 public function index(){
    $student = Student::avg('marks');

    dd($student);
  }
}

Laravel eloquent sum() method

How to calculate the sum of all values of a column in laravel than use eloquent aggregate function sum(). more explain by an example take Employee model and calculate the sum of salary by all employees by taking salary column in eloquent sum method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;

class EmployeeController extends Controller{
 public function index(){
    $employee = Employee::sum('salary');

    dd($employee);
  }
}

Laravel eloquent min() method

How to calculate minimum value of an attribute than use eloquent min aggregate function. more explain by an example take Employee model and calculate minimum salary of eloloyees by min method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;

class EmployeeController extends Controller{
 public function index(){
    $employee = Employee::min('salary');

    dd($employee);
  }
}

Laravel eloquent max() method

Laravel eloquent max aggregate funation work same as min function. It get maximum value of given column. Let more explain by example, In example return a maximum value of salary in Employee model.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;

class EmployeeController extends Controller{
 public function index(){
    $employee = Employee::max('salary');

    dd($employee);
  }
}

Laravel eloquent count() method

Laravel eloquent count function use for counting model data of table rows. We use simple count method with a model and it give counting result. Let an example

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;

class EmployeeController extends Controller{
 public function index(){
    $employee = Employee::where('status','1')->count();

    dd($employee);
  }
}

also read all aggregate function in laravel query builder

You read this tutorial on advanced web tutorial. here we provide laravel 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