How to Get Min Value of Column in Laravel?

Jan 11, 2023

.

Admin

How to Get Min Value of Column in Laravel?

Hi Guys,

I'd like to demonstrate how to find the minimum value of a column in Laravel today. I'll present a straightforward example and provide a solution if you have a question regarding laravel get row with min value. We will assist you by providing a sample of how to obtain a column's minimum value in Laravel. You will discover how to obtain the minimum value from a collection using Laravel in this article.

In Laravel, there are several ways to obtain the minimal id. I'll give you three examples utilising the orderBy(), oldest(), and min() methods. So, let's look at the sample one by one.

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...

#Laravel