Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel avg in eloquent query

How to use avg in Laravel eloquent query with example

Aman Jain, January 30, 2022January 30, 2022

In this article we will learn to use aggregate function avg in laravel eloquent and query builder. Laravel itself provide inbuilt method to avg the columns like in MySQL or SQL based database we can do avg of columns using avg aggregate function.

Avg method will give you the average of columns. For example if you have 10 rows and you want the average from a column X then you will get the average percentage from the 10 rows

Laravel eloquent or query builder avg method do the same as aggregate function. Laravel avg accepts one parameter as column name.

Here is the syntax

Model::avg(COLUMN_NAME);

//or

Model::select(\DB::raw("avg(COLUMN_NAME), other_columns "))->get(); // or first() 

//or

\DB::table('table_name')->avg(COLUMN_NAME);

//or

\DB::table('table_name')->select(\DB::raw("avg(COLUMN_NAME), other_columns "))->first()

In the above syntax we have used 4 examples to show all possibilities to use avg aggregate function in laravel.

First is using the Model and avg method of eloquent. Second is Model with select and Db::raw class.

In the third example we have used Query Builder DB class and table function to use avg method of it.

And final example is query builder with select and db raw to use avg method of MySQL or SQL.

Result of above query will be:

select avg(column_name) from  `table`  

//OR

select avg(column_name), other_columns from `table`
 

Let’s understand laravel avg in eloquent query with example

Example 1 – Laravel avg aggregate function query using eloquent avg method

So in this example i will use laravel query builder or eloquent avg method to do the avg of column.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::avg('like_count');
       \\or
       DB::table('articles')->avg('like_count');
   }
}

Output will be :

select avg(`like_count`) from `articles`

Example 2 – Laravel avg query using raw query

In this example i will use laravel query builder DB::raw method with select to avg the column

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select(\DB::raw('avg(like_count), aticles.*'))->get();
     
       \\or
        $Article = Article::selectRaw( 'avg(like_count), articles.*')->get();
     
      \\OR
       DB::table('articles')->select(\DB::raw('avg(like_count), articles.*'))->first();
   }
}

In the above example we have used articles table and created a model in our laravel application. Then we imported the necessary classes and used the avg method.

Output will be :

select avg(`like_count`), `aticles`.`*` from `articles`

Also Read : Laravel max in eloquent query with example

Related

Php Laravel avglaravelmysql

Post navigation

Previous post
Next post

Related Posts

Laravel Get Specific Columns Using with() function in laravel

How to get specific columns using with function in laravel ?

November 8, 2023March 16, 2024

When working with Laravel, a popular PHP framework, you’ll often need to retrieve specific columns using with() function in laravel from your database tables. Laravel provides a powerful and efficient way to do this using the with() function. This function allows you to specify which related models and their columns you want…

Read More
Php How to Export or Convert Html to Excel or CSV in laravel 8 9

How to Export or Convert Html to Excel or CSV in laravel 8 / 9?

May 6, 2022May 13, 2022

Excel or CSV are used to store large set of data to analyses and for reporting. In this article we will learn to export excel or CSV in laravel. This tutorial is best fit to you if you want to understand the basic of export of database table content with…

Read More
Php How to use conditional validation Laravel 8

How to use conditional validation Laravel 8 / 9 ?

December 17, 2021March 19, 2022

In Laravel sometime you want to exclude some validations on specific condition. Laravel provides exclude_if, exclude_unless and sometimes validation rules to validating some rules conditionally. In this tutorial we will take a simple example of has_city is checked then city name should be require and if its not checked then…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version