Laravel 5 Collections: Calculating the Minimum Value of a Collection With min

April 22, 2018 —John Koster

The min method is the logical opposite of the max method, and accepts the same number of arguments. The min method can be used to retrieve the maximum value of the given $key. By default, the $key is null and will function in a similar way to PHP's min function.

#Signature

1public function min(
2 $callback = null
3);

#Example Use

The following code example highlights the usage of min without specifying a $key:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 0, 1, 2, 3, 4, 5
6]);
7 
8// 0
9$min = $collection->min();

After the above code has executed the $min variable would contain the value 0. The following code example shows how to use the min function when specifying a $key:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 ['name' => 'Laravel', 'version' => '5.1'],
6 ['name' => 'Lumen', 'version' => '5.0']
7]);
8 
9// 5.0
10$min = $collection->min('version');
11 
12// Laravel
13$minName = $collection->min('name');

After the above code has executed the $min variable would contain the value 5.0 and the $minName variable would contain the value Laravel.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.