2017-12-28 61 views
2

私はかなり長い(少なくとも私にとっては)一連の条件文を持つブレードを持っています。現時点では、次のようになります。すべてのことの中央にLaravel内のステートメントがtrueの場合、カウントが返されます

<table> 
    <tbody> 
     @foreach($payments as $payment) 

    <tr> 
    <td style="width:120px;">{{$payment->id}}</td> 
<td>@if($payment->payer_id){{$payment->payer->customer_name}}@endif</td> 
<td style="width:120px;">{{$payment->payment_amount}}</td><td>{{$payment->payment_distributions->count()}} 
     @if($payment->payment_distributions->count()>0) 
      @foreach($payment->payment_distributions as $pd) 
       @if($pd->amount > 0) 
        @if($pd->shipment->balance) 
         @if($pd->amount < $pd->shipment->balance) 
          <small class="label pull-right bg-red">1</small> 
         @else 
         @endif 



        @else 
        @endif 

       @else 

       @endif 
      @endforeach 

      @else 
     @endif 

     </td> 
    </tr> 
     @endforeach 
    </tbody>  
    </table> 

あなたが見ることができるように、それは、重要である最も内側の文がtrueを返した場合、それは赤の1を返し、です。これはもちろん私の利益のためだけですが、私が望むのは、if文全体で何回返されたかを数えることです。赤7を返すのではなく、赤7を返すようにします。

+0

if文の内側にカウンタを追加します。ループの前に変数を0で開始し、内部でインクリメントします。 – aynber

+0

もしあなたが驚異的であるならば、それらとあなたの押し込みのレベル!偉大なリファクタリングのために、 –

答えて

4

これを行います。

@php($counter = 0) 
@foreach($payment->payment_distributions as $pd) 
    @if($pd->amount > 0 && $pd->shipment->balance && $pd->amount < $pd->shipment->balance) 
     @php($counter++) 
    @endif 
@endforeach 
<small class="label pull-right bg-red">{{ $counter }}</small> 

をこの代わりに:

@foreach($payment->payment_distributions as $pd) 
    @if($pd->amount > 0) 
     @if($pd->shipment->balance) 
      @if($pd->amount < $pd->shipment->balance) 
       <small class="label pull-right bg-red">1</small> 
      @else 
      @endif 



     @else 
     @endif 

    @else 

    @endif 
@endforeach 
+1

を投票してください。 –

0
@php 
    $count=0; 
@endphp 
@foreach($payment->payment_distributions as $pd) 
    @if($pd->amount > 0) 
     @if($pd->shipment->balance) 
      @if($pd->amount < $pd->shipment->balance) 
       @php 
        $count++; 
       @endphp 
      @else 
      @endif 
      @else 
     @endif 
    @else 
    @endif 
@endforeach 
<small class="label pull-right bg-red">{{$count}}</small> 
0

これを行う:

<table> 
<tbody> 
    @foreach($payments as $payment) 
     @php 
      $customer_name = ($payment->payer_id) ? $payment->payer->customer_name : ""; 
      $filtered_payment_distributions = $payment->payment_distributions->each(function ($pd, $key) { 
       if(($pd->amount > 0) && ($pd->shipment->balance) && ($pd->amount < $pd->shipment->balance)){ 
        return $pd; 
       } 
      }); 
     @endphp 
     <tr> 
     <td style="width:120px;">{{$payment->id}}</td> 
     <td>{{$customer_name}}</td> 
     <td style="width:120px;">{{$payment->payment_amount}}</td> 
     <td> 
      {{$payment->payment_distributions->count()}} 
      @if($filtered_payment_distributions->count() > 0) 
       <small class="label pull-right bg-red">{{$filtered_payment_distributions->count()}}</small> 
      @endif 
     </td> 
     </tr> 
    @endforeach 
</tbody>