我怎么能在第一次出现时停下来? - Laravel

时间:2017-01-09 08:34:03

标签: php mysql laravel

我正面临着Laravel的一个问题,因为我试图让它在第一次出现时停止。因此,从下面的代码中可以看出,我的刀片文件中有不同类型的政府。问题是,它循环通过该类型的所有政府角色并且它触发计数< 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

当前输出: 目前没有此类别的候选人。目前没有此类别的候选人。目前没有此类别的候选人。目前没有此类别的候选人。目前没有此类别的候选人。目前没有此类别的候选人。目前没有此类别的候选人。

我想要的输出: 目前没有此类别的候选人。

2 个答案:

答案 0 :(得分:0)

只需使用@break指令退出循环:

@if (count($governmentRole->stats) < 1)
    There are currently no candigates working in this category.
    @break
@else

答案 1 :(得分:0)

您可以使用forelse执行此操作。如果集合为空,forelse将遍历集合或显示消息。

@forelse($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>
@empty
    There are currently no candigates working in this category.
@endforelse

<强>更新

您必须在开始循环之前进行检查并忽略空结果集。您可以使用Laravel Collections检查集合是否包含任何结果。像这样:

@if ($royalty->count() < 1)
    We couldn't find any government roles for this category.
@elseif ($royalty->filter(function ($r) { return $r->stats->count() > 0; })->isEmpty()
    There are currently no candigates working in this category.
@else
    @foreach($royalty as $key => $governmentRole)
        <!-- Your loop code, no 'if' needed anymore -->
    @endforeach
@endif