我正面临 Laravel 的问题,因为我试图让它在第一次出现时停止。因此,正如您从下面的代码中看到的,我的 blade 文件中有不同类型的政府。问题是,它循环遍历该类型的所有政府角色,并多次触发 < 1 的计数,看起来不太友好。
代码:
<div class="col-md-8">
<div class="tab-content">
<div class="tab-pane active" id="higher_government_team">
<div class="panel panel-info">
<div class="panel panel-body">
@if ($royalty->count() < 1)
We couldn't find any government roles for this category.
@else
@foreach($royalty as $key => $governmentRole)
@if (count($governmentRole->stats) < 1)
There are currently no candigates working in this category.
@else
@foreach($governmentRole->stats as $governmentMember)
<div class="col-md-10">
<div class="col-md-12" style="margin-left:-40px;">
<div class="col-md-1" style="margin-top:-16px;"><img src="http://mywebsite.com/os734zl?figure=ch-3030-92.hr-681-34.hd-209-8.lg-3116-106-1408&size=b&direction=3&head_direction=3"></div>
<div class="col-md-9" style="margin-left:40px;">
<h4>{{ $governmentMember->user->username }} <small>{{ $governmentRole->government_title }}</small></h4>
<p><font color="#aaa">{{ $governmentRole->government_department }}</font></p><br>
</div>
</div>
</div>
@endforeach
@endif
@endforeach
@endif
@if ($higherGovernment->count() < 1)
We couldn't find any government roles for this category.
@else
@foreach($higherGovernment as $governmentRole)
@if (count($governmentRole->stats) < 1)
There are currently no candigates working in this category.
@else
@foreach($governmentRole->stats as $governmentMember)
<div class="col-md-10">
<div class="col-md-12" style="margin-left:-40px;">
<div class="col-md-1" style="margin-top:-16px;"><img src="http://mywebsite.com/os734zl?figure=ch-3030-92.hr-681-34.hd-209-8.lg-3116-106-1408&size=b&direction=3&head_direction=3"></div>
<div class="col-md-9" style="margin-left:40px;">
<h4>{{ $governmentMember->user->username }} <small>{{ $governmentRole->government_title }}</small></h4>
<p><font color="#aaa">{{ $governmentRole->government_department }}</font></p><br>
</div>
</div>
</div>
@endforeach
@endif
@endforeach
@endif
</div>
</div>
</div>
<div class="tab-pane" id="senior_government_team">
<div class="panel panel-info">
<div class="panel panel-body">
@if ($seniorGovernment->count() < 1)
We couldn't find any government roles for this category.
@else
@foreach($seniorGovernment as $governmentRole)
@if (count($governmentRole->stats) < 1)
There are currently no candigates working in this category.
@else
@foreach($governmentRole->stats as $governmentMember)
<div class="col-md-10">
<div class="col-md-12" style="margin-left:-40px;">
<div class="col-md-1" style="margin-top:-16px;"><img src="http://mywebsite.com/os734zl?figure=ch-3030-92.hr-681-34.hd-209-8.lg-3116-106-1408&size=b&direction=3&head_direction=3"></div>
<div class="col-md-9" style="margin-left:40px;">
<h4>{{ $governmentMember->user->username }} <small>{{ $governmentRole->government_title }}</small></h4>
<p><font color="#aaa">{{ $governmentRole->government_department }}</font></p><br>
</div>
</div>
</div>
@endforeach
@endif
@endforeach
@endif
</div>
</div>
</div>
<div class="tab-pane" id="junior_government_team">
<div class="panel panel-info">
<div class="panel panel-body">
@if ($juniorGovernment->count() < 1)
We couldn't find any government roles for this category.
@else
@foreach($juniorGovernment as $governmentRole)
@if (count($governmentRole->stats) < 1)
There are currently no candigates working in this category.
@else
@foreach($governmentRole->stats as $governmentMember)
<div class="col-md-10">
<div class="col-md-12" style="margin-left:-40px;">
<div class="col-md-1" style="margin-top:-16px;"><img src="http://mywebsite.com/os734zl?figure=ch-3030-92.hr-681-34.hd-209-8.lg-3116-106-1408&size=b&direction=3&head_direction=3"></div>
<div class="col-md-9" style="margin-left:40px;">
<h4>{{ $governmentMember->user->username }} <small>{{ $governmentRole->government_title }}</small></h4>
<p><font color="#aaa">{{ $governmentRole->government_department }}</font></p><br>
</div>
</div>
</div>
@endforeach
@endif
@endforeach
@endif
</div>
</div>
</div>
</div>
</div>
这部分代码大约触发了 10 次,我只想触发一次,这样看起来干净友好。我希望它为每个选项卡面板触发一次。
@if (count($governmentRole->stats) < 1)
There are currently no candigates working in this category.
@else
当前输出: 目前没有候选人在这个类别中工作。目前没有候选人在这个类别中工作。目前没有候选人在这个类别中工作。目前没有候选人在这个类别中工作。目前没有候选人在这个类别中工作。目前没有候选人在这个类别中工作。目前没有从事此类别的候选人。
我想要什么输出: 目前没有从事此类别的候选人。
最佳答案
只需使用 @break退出循环的指令:
@if (count($governmentRole->stats) < 1)
There are currently no candigates working in this category.
@break
@else
关于php - 我怎样才能在第一次出现时停止? - 拉拉维尔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543904/