How to break a for each loop in laravel blade view

0 votes

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 ?

Apr 6, 2020 in Laravel by kartik
• 37,510 points
12,700 views

1 answer to this question.

0 votes

Hello @kartik,

By default, blade doesn't have @break and @continue which are useful to have. So that's included.

Furthermore, the $loop variable is introduced inside loops, (almost) exactly like Twig.

Basic Example

@foreach($stuff as $key => $val)
     $loop->index;       // int, zero based
     $loop->index1;      // int, starts at 1
     $loop->revindex;    // int
     $loop->revindex1;   // int
     $loop->first;       // bool
     $loop->last;        // bool
     $loop->even;        // bool
     $loop->odd;         // bool
     $loop->length;      // int

    @foreach($other as $name => $age)
        $loop->parent->odd;
        @foreach($friends as $foo => $bar)
            $loop->parent->index;
            $loop->parent->parentLoop->index;
        @endforeach
    @endforeach 

    @break

    @continue

@endforeach

you can also break like this

 @foreach($data as $d)
        @if($d=="something")
            {{$d}}
            @if(codition)
              @break
            @endif

        @endif
    @endforeach
answered Apr 6, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
3,854 views
0 votes
1 answer

How to call a controller function inside a view in laravel 5

Hello @kartik, Just try this in your view ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,880 points
7,962 views
0 votes
1 answer
0 votes
1 answer

How to mock a static facade methods in Laravel?

Hey, Facades provide a "static" interface to classes ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
5,929 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,704 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,630 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,486 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,147 views
0 votes
1 answer

How to include a sub-view in Blade templates in Laravel?

Hello @kartik, You can use the blade template ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,880 points
14,484 views
+1 vote
1 answer

How to load blade or php content into a view via ajax/jquery in laravel?

Hello @kartik, Assuming you're using jQuery... create a route ...READ MORE

answered Apr 14, 2020 in Laravel by Niroj
• 82,880 points
26,890 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP