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

时间:2017-12-28 16:13:58

标签: php laravel laravel-5 count conditional

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

    <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个红色1,我希望它只返回一个红色7.

3 个答案:

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

答案 2 :(得分: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>