How to display something only for the first item from the collection in Laravel Blade template

How to display something only for the first item from the collection in Laravel Blade template

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling the First Item in Laravel Blade Loops

When rendering a list of items in a Blade template, you often need to apply distinct styling or logic to the very first element. This is common for headers, "new" badges, or breaking out of a loop to show summary information. The challenge lies in doing this without duplicating the rendering logic or creating an awkward if block at the start of your file.

The most straightforward approach uses a counter variable within the @foreach directive. Laravel's syntax allows you to pass a key argument to the loop, which acts as an automatic iterator starting from zero. You can then check if this index equals 0.

Here is how you modify your existing code:

@foreach($items as $index => $item)
    @if($index === 0)
        <div class="first-item-header">
            <h4>{{ $item->program_name }}</h4>
        </div>
    @else
        <div class="standard-item">
            <h4>{{ $item->program_name }}</h4>
        </div>
    @endif
@endforeach

In this example, the $index variable captures the position of the current item. The conditional @if($index === 0) ensures that the special formatting logic only executes for the initial element. For every subsequent iteration, the @else block handles the standard rendering. While this introduces some repetition in the HTML tags, it keeps the logic contained within a single loop structure and avoids the need to calculate offsets elsewhere.

Using first() Helper Methods

If your collection is an array or a Laravel Collection object rather than a raw PHP array, you can leverage the built-in first() method. This approach separates the header rendering from the main list entirely, which often leads to cleaner code when the first item requires significantly different structure than the rest.

You can iterate over the remaining items using skip(1) or simply loop through the collection while handling the first element outside the loop:

@if(count($items) > 0)
    @php $firstItem = $items->first(); @endphp

    {{-- Render unique header for the first item --}}
    <h4>{{ $firstItem->program_name }}</h4>

    {{-- Render the rest of the items --}}
    @foreach($items->skip(1) as $item)
        <div class="standard-item">
            <h4>{{ $item->program_name }}</h4>
        </div>
    @endforeach
@endif

This pattern is particularly effective when the first item represents a group header or a summary that shouldn't be treated as a standard row in the table. By extracting it, you prevent issues where CSS classes meant for rows might accidentally apply to a block-level header element. Note that this method relies on the collection having at least one item; wrapping the logic in a count check prevents errors on empty datasets.

Performance Considerations

While checking $index === 0 inside a @foreach loop is efficient, it does add a conditional branch for every iteration. In applications displaying thousands of items, this overhead is negligible compared to database queries or rendering heavy components. However, if you are dealing with massive datasets in high-traffic endpoints, minimizing PHP execution time becomes important.

In such scenarios, splitting the data into two separate variables (the first item and the rest) might actually be faster because it allows the @foreach loop to run without any internal conditionals, relying instead on a single initial method call. The trade-off here is increased code verbosity and potential duplication of logic if the rendering for the "rest" and the "first" becomes too similar.

When to Avoid Counting

Be cautious when using this pattern with generators or iterators that cannot be indexed directly, though Laravel's standard foreach usually handles arrays and collections seamlessly. If you are working with a generator function passed into Blade, converting it to an array first (using iterator_to_array()) destroys the ability to skip elements efficiently without consuming memory. Stick to the counting method for simple lists where readability is paramount, and reserve the extraction pattern for cases where structural differences between the first item and the rest are substantial.

For more complex list manipulations or if you find yourself needing advanced iteration controls, the official Laravel documentation on loops provides further examples on utilizing prepend and append methods to alter collection order before rendering.

Our network