如何在拉拉维尔叶片视图中打破前方循环?

2022-08-30 10:23:18

我有一个这样的循环:

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

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

如何在拉拉维尔叶片视图中实现?


答案 1

来自 Blade 文档

使用循环时,您还可以结束循环或跳过当前迭代:

@foreach ($users as $user)
    @if ($user->type == 1)
        @continue
    @endif

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

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

答案 2

你可以像这样打破

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

推荐