【问题标题】:Laravel: How to move a new line when the value is not the same as the previous value, foreach in bladeLaravel:当值与前一个值不同时如何移动新行,刀片中的foreach
【发布时间】:2019-12-27 04:02:25
【问题描述】:
在做循环过程的时候,想比较一下当前值和前一个值不一样的时候,再换一行。
--------------------------
|unique_id| name | stats |
--------------------------
|1 | A | P |
|1 | B | P |
|1 | C | P |
|2 | D | P |
--------------------------
这是我在刀片中的代码:
@foreach ($result as $item)
{{ $item->stats}}
@endforeach
结果:
【问题讨论】:
标签:
php
laravel
laravel-5.8
【解决方案1】:
在foreach 之外定义$previousstatus。一旦foreach 运行,该变量将设置当前status 的值,这将在下一次运行时对您有所帮助。
@php $previousstatus = ''; @endphp
@foreach ($result as $item)
//@if($previousstatus == $item->stats) @endif do as you want with previousstatus
{{ $item->stats}}
@php $previousstatus = $item->stats; @endphp //set the status for next loop becuase we need to remember in next loop what was the last element so store it in temp(previousstatus ) varible,
@endforeach