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

Laravel min in eloquent query with example

Aman Jain, January 23, 2022January 23, 2022

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

Min method will give you the most min number across the columns. For example if you have 10 rows and you want the minimum number from a column X then you will get the min number from the 10 rows

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

Here is the syntax

Model::min(COLUMN_NAME);

//or

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

//or

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

//or

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

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

First is using the Model and min 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 min method of it.

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

Result of above query will be:

select MIN(column_name) from  `table`  

//OR

select MIN(column_name), other_columns from `table`
 

Let’s understand laravel min in eloquent query with example

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

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

Output will be :

select min(`like_count`) from `articles`

Example 2 – Laravel min query using raw query

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

Output will be :

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

Also Read : Laravel max in eloquent query with example

Related

Php Laravel laravelminmysql

Post navigation

Previous post
Next post

Related Posts

How to increase maximum file upload size in php ?

August 24, 2021August 24, 2021

Php is the most used language in web. sometimes it required to upload the files which is greater then 2 mb. In php default upload size is 2 mb and if we wanted to increase it then we have multiple ways to change the size of uploading. 1. Change from…

Read More
Php Laravel custom login and register

Laravel 8 Custom login and registration with example

December 3, 2021December 3, 2021

In Laravel 8 there is multiple ways to implement the login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can say we…

Read More

How many type of methods in Restful api?

August 28, 2021February 22, 2024

Restful api is very useful today while creating a mobile application or web application. Restful api or Http request methods There is 4 types of method of http request. Post Get Put Delete Patch 1. Post method Post method is widely used to fetch the content of a form or…

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