关于php:如何在laravel刀片视图中打破foreach循环?

How to break a foreach loop in laravel blade view?

我有一个这样的循环:

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

如果条件满足,我想在数据显示后中断循环。

如何在Laravel刀刃视图中实现?


从Blade文档:

When using loops you may also end the loop or skip the current
iteration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@foreach ($users as $user)
    @if ($user->type == 1)
        @continue
    @endif

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


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


Basic usage

默认情况下,刀片没有@break@continue,这是有用的。包括在内。

此外,EDCOX1×2变量在环路内被引入,(几乎)完全像树枝。

Basic Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@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


你可以这样打破

1
2
3
4
5
6
7
8
9
 @foreach($data as $d)
        @if($d=="something")
            {{$d}}
            @if(codition)
              @break
            @endif

        @endif
    @endforeach

默认情况下,刀片没有@break,但您可以使用这些Laravel刀片扩展来获得您想要的:

http://robin.radic.nl/blade-extensions/directives/foreach.html