I think I break all the rules by deliberately creating a repeating question ...
another question has an accepted answer. This, obviously, solved the problem of the aderists, but it did not answer the title question.
Let it start from the beginning - the method first()is implemented like this:
foreach ($collection as $item)
return $item;
This is obviously more sustainable than accepting $collection[0]or using other proposed methods.
There cannot be an element with an index 0or index 15, even if the collection has 20 elements. To illustrate the problem, open this collection from the docs:
$collection = collect([
['product_id' => 'prod-100', 'name' => 'desk'],
['product_id' => 'prod-200', 'name' => 'chair'],
]);
$keyed = $collection->keyBy('product_id');
Now do we have any reliable (and, preferably, compressed) way to access the nth element $keyed?
:
$nth = $keyed->take($n)->last();
($keyed->last()) , $n > $keyed->count(). n- , , null, first()?
, :
$col = collect([
2 => 'a',
5 => 'b',
6 => 'c',
7 => 'd']);
$col->first(). ?
$col->nth(3) 'c' ( 'c', 0, first()). $col[3] , .
$col->nth(7) null, , . $col[7] , 'd'.
" n- foreach?" .