How to use Aggregate methods in Laravel 5

The query builder also provides a variety of aggregate methods such as count, max, min, avg, and sum. You may call any of these methods after constructing your query.

Aggregate method is a method where the values of multiple rows are grouped together as input on certain criteria to form a single value of more significant meaning or measurements such as a set, a bag or a list.

Count method

$count = DB::table('tbl_add_post')->count();

Min method

$amount = DB::table('tbl_orders')->min('amount');

Max method

$amount = DB::table('tbl_orders')->max('amount');

Sum method

$amount = DB::table('tbl_orders')->sum('amount');

Avg method

$amount = DB::table('tbl_orders')
                ->where('status', 1)
                ->avg('amount');

Leave a Reply

Your email address will not be published. Required fields are marked *