【问题标题】:Counting returned true if statements in Laravel如果 Laravel 中的语句计数返回 true
【发布时间】:2017-12-28 16:13:58
【问题描述】:

我有一个带有相当长(至少对我而言)一组条件语句的 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。

【问题讨论】:

  • 在 if 语句中添加一个计数器。在循环之前从 0 开始变量,并在内部递增。
  • 你的缩进水平和那些 if elses 太棒了!

标签: php laravel laravel-5 count conditional


【解决方案1】:

这样做:

@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

【讨论】:

    【解决方案2】:
    @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>
    

    【讨论】:

      【解决方案3】:

      这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 2016-02-12
        • 2019-08-26
        • 2016-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多