Laravel - Blade Templates - Loops

In addition to conditional statements, Blade provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:

    @for ($i = 0; $i < 10; $i++)
        The current value is {{ $i }}
    @foreach ($users as $user)

This is user {{ $user->id }}

@endforeach @forelse ($users as $user)
  • {{ $user->name }}
  • @empty

    No users

    @endforelse @while (true)

    I'm looping forever.

    When looping, you may use the loop variable to gain valuable information about the loop, such as whether you are in the first or last iteration through the loop.

    When using loops you may also end the loop or skip the current iteration using the @continue and @break directives:

        @foreach ($users as $user)
            @if ($user->type == 1)
  • {{ $user->name }}
  • @if ($user->number == 5) @break @endif @endforeach

    You may also include the continuation or break condition within the directive declaration:

        @foreach ($users as $user)
            @continue($user->type == 1)
  • {{ $user->name }}
  • @break($user->number == 5) @endforeach