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
Example #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());
 }
 /**
  * Generate a unique suffix for the given slug (and list of existing, "similar" slugs.
  *
  * @param string $slug
  * @param string $separator
  * @param \Illuminate\Support\Collection $list
  * @return string
  */
 protected function generateSuffix($slug, $separator, Collection $list)
 {
     $len = strlen($slug . $separator);
     // If the slug already exists, but belongs to
     // our model, return the current suffix.
     if ($list->search($slug) === $this->model->getKey()) {
         $suffix = explode($separator, $slug);
         return end($suffix);
     }
     $list->transform(function ($value, $key) use($len) {
         return intval(substr($value, $len));
     });
     // find the highest value and return one greater.
     return $list->max() + 1;
 }