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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?