How to break a foreach loop in laravel blade view?

Viewed 104207

I have a loop like this:

@foreach($data as $d)
    @if(condition==true)
        {{$d}}
        // Here I want to break the loop in above condition true.
    @endif
@endforeach

I want to break the loop after data display if condition is satisfied.

How it can be achieved in laravel blade view ?

5 Answers

Official docs say: 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)
    @continue
@endif

<li>{{ $user->name }}</li>

@if ($user->number == 5)
    @break
@endif

@endforeach

@foreach($data as $d)
    @if(condition==true)
        {{$d}}
        @break // Put this here
    @endif
@endforeach
Related