ItSolutionStuff.com

Laravel Get Min Value of Column Example

By Hardik Savani • November 5, 2023
Laravel

Hi Guys,

Today, I would like to show you laravel get min value of column. If you have a question about laravel get row with min value then I will give a simple example with a solution. we will help you to give an example of get min value of a column in laravel. Here you will learn get min value from collection laravel.

There are several ways to get get minimum id in laravel. i will give you three example using min(), oldest() and orderBy() method. so, let's see the one-by-one example.

Example 1: using min()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Product;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$minID = Product::whereNotNull('price')->min("price");

dd($minID);

}

}

Output:

70.00

Example 2: using orderBy()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Product;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$minID = Product::whereNotNull('price')->orderBy('price', 'asc')->value('price');

dd($minID);

}

}

Output:

70.00

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Copy Record using Eloquent Replicate Example

Read Now →

Laravel Eloquent Sum Multiple Columns Example

Read Now →

Laravel Eloquent firstOr() Example

Read Now →

Laravel Eloquent inRandomOrder() Method Example

Read Now →

Laravel Eloquent whereNull() Query Example

Read Now →

Laravel Eloquent selectRaw() Query Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

Multiple orWhere Condition in Laravel Eloquent

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

Laravel One to One Eloquent Relationship Tutorial

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →