How to get Average Value of a Column from Database in Laravel
In this article we are going to learn how can you get the average of any column of your database in Laravel . To calculate average of a column in Laravel is very easy but we just need to know the way to write the query . In this article we will see the Laravel Eloquent method for calculating average from Database .
No Doubt , During the calculation of average you can use multiple where condition and other helper functions and aggregate functions according to your need .
Syntax 1: -
$data = DB::table('table_name')
->where('column_name', 'value')
->avg('column_name');
Example 1: -
Syntax 2: -
$data = DB::table('table_name')
->where(['column_name' => 'value'])
->avg('column_name');
Example 2: -
Syntax 3: -
$data = Model_Name::avg('Column_name');
Example 3: -
These were the three ways to use average query in Laravel.
Thank you for reading this article 😊
For any query do not hesitate to comment 💬
Also Read : -
Laravel Eloquent SQL Update QueryHow to add Search functionality in Laravel
Laravel 7/6 Email Verification
Add Date-Time Picker in Laravel
Conversion of Array to String in Laravel
Why Laravel is the Most Popular PHP Framework