Laravel: 当值与之前的值不一样时如何移动新行,foreach in blade

Laravel: How to move a new line when the value is not the same as the previous value, foreach in blade

在做循环过程的时候,我想比较一下当前值和之前的值不一样,然后换行。

--------------------------
|unique_id| name | stats |
--------------------------
|1        | A    | P     |
|1        | B    | P     |
|1        | C    | P     |
|2        | D    | P     |
--------------------------

这是我在 blade 中的代码:

@foreach ($result as $item)
  {{ $item->stats}}
@endforeach

结果:

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