Calculating Column Max Value Example In Laravel

Hello Artisan

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

Do you know how to find maximum value from a column in laravel ? In this tutorial i will show it using laravel collection max() method. Laravel collection has many method to find many problemic thing easily. In this lecture we will find just max value from a column. 

So you will see select row with max value laravel using laravel collection method. Let's have a look

Example 1 : max()

Let's think we have a simple array and we would like to find out the max 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)->max();

});

 

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 
30

 

Example 2 : max()

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

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

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

});

Ourput will be the same. 

 

Example 3 : max()

Now have a look on a little bit advance query. Think we have to property in an array We have to find out specific max from single field and then we have to add those max. 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)->max(function($value) {
        return $value["price"] + $value["tax"];
   });

});
//Output
45

Output will be 45. because from price we get max is 30 and from tax we get 10 max 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 : max()

Now we will add one more field like active. So we only find out the max value 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)->max(function($value) {

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

Output will be  45. because from price we get 30 max and from tax we get 15 max and then finally we have to add it.

So we have passed a closer to max function with value parameter and then add it. Here we find out only active filed max and add it finally.