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

Laravel max in eloquent query with example

Aman Jain, January 23, 2022November 7, 2023

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

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

Here is the syntax

Model::max(COLUMN_NAME);

//or

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

//or

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

//or

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

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

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

In the third example we have used QueryBuilder DB class and table function to use max method of it.

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

Result of above query will be:

select MAX(column_name) from  `table`  

//OR

select MAX(column_name), other_columns from `table`
 

Laravel max using native eloquent and raw query

Let’s understand laravel max in eloquent query with example

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

So in this example i will use laravel query builder or eloquent max method to do the max 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::max('like_count');
       \\or
       DB::table('articles')->max('like_count');
   }
}

Output will be :

select max(`like_count`) from `articles`

Example 2 – Laravel max query using raw query

In this example i will use laravel query builder DB::raw method with select to max 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('max(like_count), aticles.*'))->get();
     
       \\or
        $Article = Article::selectRaw( 'max(like_count), articles.*')->get();
     
      \\OR
       DB::table('articles')->select(\DB::raw('max(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 max method.

Output will be :

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

Also Read : Laravel sum in eloquent query with example

Related

Php Laravel laravelmaxmysql

Post navigation

Previous post
Next post

Related Posts

What is Access Modifiers ?

July 18, 2021August 2, 2021

In object oriented programming(oops) to set the visibility of classes, variables and methods we are using access modifiers. So Access modifiers are used the encapsulation of class properties. There are three types of access modifiers in any language (Php, java or c etc. ) Private Protected Public 1. Private Access…

Read More
Php How to Implement Remember Me Feature in Laravel

How to Implement Remember Me Feature in Laravel ?

August 12, 2022March 16, 2024

Laravel authentication offers remember me functionality out of the box and to implement remember me feature in laravel we only need to follow some guidelines provided by laravel so in this article i will show you the simplest way to implement this. Most of the time while developing login functionality of application…

Read More
Php How to access and setup storage files after upload in laravel

How to access and setup storage files after upload in laravel ?

May 23, 2022May 26, 2022

In this article i will show you to access and setup uploaded storage files in laravel. This is strongly recommended that not to use direct URL from storage, laravel internally handle it by creating Symblink but some developers access the files of storage directly without knowing the consequences of website…

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

  • August 2025
  • July 2025
  • 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 Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version