How to Find Column Min Values in Laravel

Hello Artisan

In this tutorial i will show you how we can find min value from a collumn in laravel using laravel collection min() method. If you don't know about laravel collection method, then you are a right place. I will teach you from beginner to advance.

So you will see laravel collection min value using laravel collection method. We can find it in many ways. But in this tutorial i will use laravel collection min() method to find minimum value. Let's have a try.

 

Example 1 : min()

Let's think we have a simple array and we would like to find out the min value from this array. How we can do it using laravel collection. Let's have a look on the below example.

Route::get('/', function () {

   $data = [
        10,
        20,
        30
    ];

   return collect($data)->min();

});

 

For example, check out the following code. We'll use the collect helper to create a new collection instance from the array.

Look, we have a data property object and we just push it into laravel collection collcet() helper method and just call the max function to find out the max values. What will be the output? 

//output 
10

 

Example 2 : min()

We can also pass parameter to min function. see example.

Route::get('/', function () {

     $data = [
        ["price" => 10],
        ["price" => 20],
        ["price" => 30]
    ];
  return collect($data)->min("price"); 

});

Ourput will be the same. 

 

Example 3 : min()

Now have a look on a little bit advance query. Think we have to property in an array We have to find out specific min from single field and then we have to add those min value. How can we do that ? See the below example.

Route::get('/', function () {

   $data = [
        ["price" => 10, "tax" => 5],
        ["price" => 20, "tax" => 10],
        ["price" => 30, "tax" => 15]
    ];

   return collect($data)->min(function($value) {
        return $value["price"] + $value["tax"];
   });

});

Output will be 15. because from price we get min is 10 and from tax we get 5 min and then finally we have to add it. So we have passed a closer to average function with value parameter and then add it. 

 

Example 4 : min()

Now we will add one more field like active. So we only find out the min of active field and then finally add it. See the below example

Route::get('/', function () {

    $data = [
        ["price" => 10, "tax" => 5, "active" => true],
        ["price" => 20, "tax" => 10, "active" => false],
        ["price" => 30, "tax" => 15, "active" => true]
    ];

    return collect($data)->min(function($value) {

        return !$value['active'] 
         ? null : $value["price"] + $value["tax"];
         
    });
 
 });

 

Output will be  15. because from price we get 10 min and from tax we get 5 min and then finally we have to add it.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js