Illuminate\Support\Collection::max PHP Method

max() public method

Get the max value of a given key.
public max ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed
    public function max($callback = null)
    {
        $callback = $this->valueRetriever($callback);
        return $this->reduce(function ($result, $item) use($callback) {
            $value = $callback($item);
            return is_null($result) || $value > $result ? $value : $result;
        });
    }

Usage Example

Exemplo n.º 1
0
 public function testGettingMaxItemsFromCollection()
 {
     $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
     $this->assertEquals(20, $c->max('foo'));
     $c = new Collection([['foo' => 10], ['foo' => 20]]);
     $this->assertEquals(20, $c->max('foo'));
     $c = new Collection([1, 2, 3, 4, 5]);
     $this->assertEquals(5, $c->max());
     $c = new Collection();
     $this->assertNull($c->max());
 }
All Usage Examples Of Illuminate\Support\Collection::max