Illuminate\Support\Collection::first PHP Method

first() public method

Get the first item from the collection.
public first ( callable $callback = null, mixed $default = null ) : mixed
$callback callable
$default mixed
return mixed
    public function first(callable $callback = null, $default = null)
    {
        return Arr::first($this->items, $callback, $default);
    }

Usage Example

Exemplo n.º 1
4
 public function getInitial()
 {
     $words = new Collection(explode(' ', $this->name));
     // if name contains single word, use first N character
     if ($words->count() === 1) {
         if ($this->name->length() >= $this->length) {
             return $this->name->substr(0, $this->length);
         }
         return (string) $words->first();
     }
     // otherwise, use initial char from each word
     $initials = new Collection();
     $words->each(function ($word) use($initials) {
         $initials->push(Stringy::create($word)->substr(0, 1));
     });
     return $initials->slice(0, $this->length)->implode('');
 }
All Usage Examples Of Illuminate\Support\Collection::first