如果Laravel中的语句,则计数返回true

如果Laravel中的语句,则计数返回true

问题描述:

I have a Blade with a rather long (at least for me) set of conditional statements. At the moment it looks like this:

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

In the middle of all that is where it is important, as you can see, it returns a red 1 if the innermost statement returns true. This is of course solely for my benefit, but what I would like is to have it count how many times within overall if statement it returns true, so rather than returning 7 red 1s, I'd like it to return just a red 7.

我有一个带有相当长的(至少对我而言)条件语句的Blade。 目前它看起来像这样: p>

 &lt; table&gt; 
&lt; tbody&gt; 
 @foreach($ payments as $ payment)
 
&lt; tr&gt;  ; 
&lt; td style =“width:120px;”&gt; {{$ payment-&gt; id}}&lt; / td&gt; 
&lt; td&gt; @if($ payment-&gt; payer_id){{$ payment  - &gt; payer-&gt; customer_name}} @ endif&lt; / td&gt; 
&lt; td style =“width:120px;”&gt; {{$ payment-&gt; payment_amount}}&lt; / td&gt;&lt; td&gt; {  {$ payment-&gt; payment_distributions-&gt; count()}} 
 @ if($ payment-&gt; payment_distributions-&gt; count()&gt; 0)
 @foreach($ payment-&gt; payment_distributions as $ pd  )
 @if($ pd-&gt; amount&gt; 0)
 @if($ pd-&gt; shipment-&gt; balance)
 @if($ pd-&gt; amount&lt; $ pd-&gt; 装运 - >平衡)
&lt; small class =“label pull-right bg-red”&gt; 1&lt; / small&gt; 
 @else 
 @endif 
 
 
 
 @else 
 @  endif 
 
 @else 
 
  @endif 
 @endforeach 
 
 @else 
 @endif 
 
&lt; / td&gt; 
&lt; / tr&gt; 
 @endforeach 
&lt; / tbody&gt;  
&lt; / table&gt; 
  code>  pre> 
 
 

在所有重要的地方中间,如您所见,如果最内层的陈述,它会返回红色1 返回true。 这当然仅仅是为了我的利益,但我想要的是让它计算在整个if语句中它返回true的次数,所以不是返回7个红色1,我希望它只返回一个红色7。 p> div>

Do this:

@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>

Instead of this:

@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

@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>

Do this :

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