How To Use Aggregates Functions With Model In Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

How To Use Aggregates Functions With Model In Laravel Framework

To countmaxminavg, and sum of the data in table

SQL count() Syntax

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

SQL min() Syntax

SELECT MIN(column_name)
FROM table_name
WHERE condition;


SQL max() Syntax
 
SELECT MAX(column_name)
FROM table_name
WHERE condition;
 
SQL avg() Syntax

SELECT AVG(column_name)
FROM table_name
WHERE condition;

 SQL sum() Syntax

SELECT SUM(column_name)
FROM table_name
WHERE condition;

To learn more about SQL, please visit our BajarangiSoft site.

How can you use aggregates function in laravel

Let's see

You may also use the countsummax, and other aggregate methods provided by the query builder. These methods return the appropriate scalar value instead of a full model instance:

Example(1)
Counts the number of user in users table

<?php

namespace App\Http\Controllers;
use App\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::where('active', 1)->count();
        return view('employee.index', ['employee' => $employee]);
    }
}
?>

Example(2)
Get the maximum salary of employee per year
<?php

namespace App\Http\Controllers;
use App\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::where('id', 1)->max('salary');
        return view('employee.index', ['employee' => $employee]);
    }
}
?>

Example(3)
Get the minimum salary of employee per year
<?php

namespace App\Http\Controllers;
use App\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::where('id', 1)->min('salary');
        return view('employee.index', ['employee' => $employee]);
    }
}
?>

Example(4)
Get the sum of salary of employee per year

 
<?php

namespace App\Http\Controllers;
use App\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::where('id', 1)->sum('salary');
        return view('employee.index', ['employee' => $employee]);
    }
}
?>

Example(5)
Get the average salary of employee per year

 
<?php

namespace App\Http\Controllers;
use App\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::where('id', 1)->avg('salary');
        return view('employee.index', ['employee' => $employee]);
    }
}
?>

Related Post