Introducing Laravel Quizzes! Play now

Working with @blade $loops

Working with @blade $loops


Everyone knows Blade files are part of the heart and soul of Laravel applications, but I feel like most people don't use them to their full potential.

There is much that could be discussed with the above statement, but for this article I'm going to focus on loops specifically.

This is how you declare regular no-frills loops in Blade:

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

@while (true)
    <p>Looping forever.</p>
@endwhile

Did you know about @forelse?

Instead of:

@if($users->count())
    @foreach($users as $user)
        <p>{{ $user->name }}</p>
    @endforeach
@else
    <p>No users</p>
@endif

Use:

@forelse ($users as $user)
    <p>{{ $user->name }}</p>
@empty
    <p>No users</p>
@endforelse

Did you know you can continue and break loops without needing the @php directive?

Instead of:

@php
    continue;
@endphp

@php
    break;
@endphp

Use:

@continue
@break

You can also give them conditions:

Instead of:

@if ($user->isAdmin())
    @continue
@endif

@if ($user->isAdmin())
    @break
@endif

Use:

@continue($user->isAdmin())
@break($user->isAdmin())

There is also an available $loop variable given to you inside all loops:

You can do things like, add a horizontal rule after all the elements except the last one:

@foreach ($posts as $post)
    {{ $post->content }}

    @if (! $loop->last)
        <hr />
    @endif
@endforeach

Or just the first one:

@foreach ($posts as $post)
    @if ($loop->first)
        <p>This is my first post!</p>
    @endif
    
    {{ $post->content }}
@endforeach

Or you can add numbers to things:

Instead of:

@foreach ($posts as $index => $post)
    <p><strong>Post {{ $index + 1 }}</strong></p>
    
    {{ $post->content }}
@endforeach

Use:

@foreach ($posts as $post)
    <p><strong>Post {{ $loop->iteration }}</strong></p>
    
    {{ $post->content }}
@endforeach

Note: $loop->iteration starts at 1, $loop->index starts at 0.

You can style a table based on if a row is even or odd:

<tr class="{{ $loop->even ? 'bg-gray-400' : '' }}"></tr>
<tr class="{{ $loop->odd ? 'bg-gray-400' : '' }}"></tr>

You can keep track of where you are in the loop:

@foreach ($posts as $post)
    <p>There are {{ $loop->remaining }} items out of {{ $loop->count }} items.</p>
@endforeach

You can even keep track of depth:

@foreach ($posts as $post)
    @foreach($post->comments as $comments)
        @foreach($comments->tags as $tag)
            <p>I am {{ $loop->depth }} levels deep.</p>
        @endforeach
    @endforeach
@endforeach

Lastly, you can get access to the parent loops:

@foreach ($posts as $post)
    @foreach($post->comments as $comment)
        @foreach($comment->tags as $tag)
            <p>{{ $loop->parent }} is the parent comment.</p>
            <p>{{ $loop->parent->parent }} is the parent post.</p>
        @endforeach
        
        @if ($loop->parent->first)
            <p>This is the first post and I am checking from the comments loop.</p>
        @endif
    @endforeach
@endforeach
Anthony Rappa

By Anthony Rappa

Hello! I'm a full stack developer from Long Island, New York. Working mainly with Laravel, Tailwind, Livewire, and Alpine.js (TALL Stack). I share everything I know about these tools and more, as well as any useful resources I find from the community. You can find me on GitHub and LinkedIn.