How to access the nth item in a Laravel collection?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# How to Access the $n^{th}$ Item in a Laravel Collection: A Developer's Guide I guess I am breaking all the rules by deliberately making a duplicate question... The desire to access an item by its position—the "nth" item—is fundamental in programming. However, when working with dynamic data structures like Laravel Collections, the method you choose drastically impacts your code's robustness and readability. Simply using array syntax often leads to brittle code that crashes when dealing with missing elements. This post will dive into why direct indexing can be problematic and demonstrate the most reliable, idiomatic ways to retrieve an item by its position in a Laravel Collection, focusing on safety and clarity. ## The Danger of Direct Indexing: Why `$collection[n]` Fails When you look at simple PHP arrays or collections, the immediate temptation is to use bracket notation, like `$collection[0]` or `$collection[5]`. While this works perfectly fine if you are absolutely certain that the index exists, it introduces significant risk. If the collection is empty, or if you request an index that is out of bounds (e.g., asking for the 10th item in a collection of only 5), PHP will throw an error, leading to unexpected application crashes. This behavior is precisely what we want to avoid in production code. A robust solution must handle edge cases gracefully, returning `null` or some default value instead of throwing an exception. ## Reliable Methods for Safe Access Since direct indexing is unreliable for positional access, developers turn to methods provided by the Collection class that emphasize safety and functional programming principles. The best approach depends heavily on whether you are looking for a specific key, the first item, or a specific element from an ordered sequence. ### 1. Using `get()` for Key-Based Access If your collection is keyed (like when using `keyBy()`), the safest way to retrieve an item by