Control Structures: The Loop Variable

Provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop.

@foreach ($users as $user)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>This is user {{ $user->id }}</p>
@endforeach
@foreach ($users as $user)
    @foreach ($user->posts as $post)
        @if ($loop->parent->first)
            This is first iteration of the parent loop.
        @endif
    @endforeach
@endforeach

Available inside of your loop.

  • $loop->index.
  • $loop->iteration.
  • $loop->remaining.
  • $loop->count.
  • $loop->first.
  • $loop->last.
  • $loop->depth.
  • $loop->parent.

Control Structures: The Loop Variable — Structure map

Clickable & Draggable!

Control Structures: The Loop Variable — Related pages: