How to access the nth item in a Laravel collection?
Based on Alexey's answer, you can create a macro in AppServiceProvider (add it inside register method):
use Illuminate\Support\Collection;Collection::macro('getNth', function ($n) { return $this->slice($n, 1)->first();});
and then, you can use this throughout your application:
$collection = ['apple', 'orange'];$collection->getNth(0) // returns 'apple'$collection->getNth(1) // returns 'orange'$collection->getNth(2) // returns null$collection->getNth(3) // returns null