如果 Laravel 中的语句计数返回 true
Counting returned true if statements in Laravel
我有一个带有相当长(至少对我而言)一组条件语句的 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>
在最重要的部分中间,如您所见,如果最里面的语句 return 为真,它 return 为红色 1。这当然完全是为了我的利益,但我想要的是让它在整个 if 语句中计算多少次它 returns true,所以而不是 returning 7 red 1s,我'我喜欢 return 只是一个红色的 7。
@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>
这样做:
@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
这样做:
<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>
我有一个带有相当长(至少对我而言)一组条件语句的 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>
在最重要的部分中间,如您所见,如果最里面的语句 return 为真,它 return 为红色 1。这当然完全是为了我的利益,但我想要的是让它在整个 if 语句中计算多少次它 returns true,所以而不是 returning 7 red 1s,我'我喜欢 return 只是一个红色的 7。
@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>
这样做:
@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
这样做:
<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>