douji1058 2017-12-28 16:13
浏览 234
已采纳

如果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.

  • 写回答

3条回答 默认 最新

  • duanmou9228 2017-12-28 16:22
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog