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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)