Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lkmadushan's avatar

break, continue, return in blade template

How to use break, continue, return statements is a blade template

0 likes
23 replies
bashy's avatar

Well, you can do normal PHP inside it but depends on why you'd want to do that... maybe a foreach break?

Marwelln's avatar

Usually you wouldn't need to, but if you must, use <?php return; ?>

bashy's avatar

I don't think there's any native support for break, no.

intrip's avatar

You need to put it inside the php tag: and so on...

eugenefvdm's avatar

I have the need to break in a foreach after the first loop. I tried:

@if (1==1) @break; @endif

but it didn't work. Instead I just see @break; repeatedly printed on the screen.

2 likes
bartimurugesan's avatar

Its simple to Use break inside the blade template. You can just put the php script inside the blade like , It will work and I used this method and got succeed.

astierler's avatar

Anyone coming in here, you can use @break and @continue inside foreach loops in Blade now. (Not sure if/how long ago this became a change, but it's valid)

dorelljames's avatar

Anyone who needs to support it such as L5.1, see below:

Inside boot method of AppServiceProvider class in app/Providers, you can define a directive like this:

public function boot()
{
    Blade::directive('break', function() { return "<?php break; ?>"; });
}

Don't forget to write use Blade; below the namespace declaration.

orrd's avatar

You can still use PHP in your Blade templates, and Blade @foreach is just implemented as a PHP foreach. So you can just do <?php break; ?>. You can also do <?php continue; ?> as well.

Whether or not that's a bad idea is up to you to decide. It's not pretty, and it would break if the way Blade implemented foreach in a different way. But if you're ok with it, then there's no reason you can't do it that way. But the Blade directive is prettier.

1 like
agbenu's avatar

This is quite late but hopefully it may help someone facing a similar problem.

@continue and @ break was added in Laravel 5.2.22.

You can now use it directly in blade

@continue($some_condition)

@break($some_condition)

Example: @continue($student->id == 20)

1 like
Finesse's avatar
@foreach ($values as $value)
    @if ($value < 0)
        @php
            continue;
        @endphp
    @endif

    {{ $value }}
@endforeach

or

@foreach ($values as $value)
    @if ($value < 0)
        @continue
    @endif

    {{ $value }}
@endforeach

Both variants works in Laravel 5.6.

sumit1405's avatar

continue, can be achieved simply by adding an if condition to the contents of loop.

gnuxdar's avatar

Hello, How can I skip the active post in the case of a blog?

Please or to participate in this conversation.