2017-12-28 12 views
2

다소 긴 (최소한 나를 위해) 조건문 집합이있는 블레이드가 있습니다. 순간 그것은 다음과 같습니다Laravel의 문에서 참이 반환됩니다.

<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 문 내에서 true를 반환하는 횟수를 세는 것입니다. 따라서 빨간색 7을 반환하는 대신 빨간색 7을 반환하고 싶습니다.

+0

if 문 안에 카운터를 추가하십시오. 루프 전에 0에서 변수를 시작하고 내부에서 변수를 증가시킵니다. – aynber

+0

당신의 들여 쓰기 수준은 elses 경우에 그것의 경이로운! 훌륭한 리펙토링을 위해 –

답변

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>  

관련 문제