April 22, 2018 —John Koster
The max
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 max
function.
1public function max(2 $callback = null3);
The following code example highlights the usage of max
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, 56]);7 8// 59$max = $collection->max();
After the above code has executed the $max
variable would contain the value 5
. The following code example shows how to use the max
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.110$max = $collection->max('version');11 12// Lumen13$maxName = $collection->max('name');
After the above code has executed the $max
variable would contain the value 5.1
and the $maxName
variable would contain the value Lumen
.
∎
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.