2017-01-09 3 views
2

私はLaravelの問題に直面しています。これは最初の発生時にこれを停止しようとしています。下のコードからわかるように、私はブレードファイルにさまざまな種類の政府を持っています。問題は、それはそのタイプのすべての政府の役割を回り、カウントは<に1回複数回発射され、それは非常にやさしく見えません。最初の発生時にどのように停止できますか? - Laravel

コード:

<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 

現在の出力: 現在のところ、このカテゴリには志願者はいません。現在、このカテゴリーで働く志願者はいません。現在、このカテゴリーで働く志願者はいません。現在、このカテゴリーで働く志願者はいません。現在、このカテゴリーで働く志願者はいません。現在、このカテゴリーで働く志願者はいません。現在、このカテゴリーで働く志願者はいません。

私が求めている出力: 現在、このカテゴリには志願者はいません。

答えて

0

だけループを終了する@breakディレクティブを使用します。

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

それ以上の政府のメンバーが処理されなくなります。 – Ashkru

+0

@breakはループを終了します。それはあなたが探しているものではない場合、私はあなたが達成しようとしていることを理解していない、申し訳ありません。 –

+0

私は「現在、このカテゴリーで働いている義理の人はいません。」をスキップしようとしています。メッセージがすでに発生している場合 – Ashkru

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 
+0

1つのタブパネルに複数の政府のタイプがある場合に役立つものは何ですか? – Ashkru

+0

また、これは、最初のforeachのために、まだ1回も@emptyを何回も繰り返すので、実際には役に立ちません。 – Ashkru