我正在循环浏览按created_at
排序的Laravel集合以制作时间表。每个项目的开始日期为 created_at
,结束日期是以下项目的created_at
。如果它是最后一项,则该事件仅持续设定的 30 天。
我当前的代码是这样的:
@foreach ($statushistory->where('itemid', $timelineitemid) as $hist)
[ '{{$hist->newstatus}}', new Date({{$hist->created_at->format('Y')}}, {{$hist->created_at->format('n')-1}}, {{$hist->created_at->format('j')}}), new Date({{$hist->created_at->format('Y')}}, {{$hist->created_at->format('n')-1}}, {{$hist->created_at->format('j')}}) ],
@endforeach
正如您在此处看到的,结束日期与开始日期相同,但我需要它是下一项的开始日期。我以为会有一个像last()
这样的帮手,我可以称之为next()
或其他什么。由于没有数字键,我不能只添加一个并抓住它。该项目按日期排序,唯一的 ID 类型字段是数据库中的字段,它是随机 ID,如 1434 或 1356。
关于如何获取下一项start_date并检查我们是否在最后一项的任何想法?我查看了PHP的next
函数,但我认为它不能满足我的需要。
非常感谢
你必须先获取迭代器:
$collection = $collection->getIterator();
$current = current($collection);
$next = next($collection);
该集合将由递增索引键控,因此您应该能够执行...
$collection = $statushistory->where('itemid', $timelineitemid);
foreach ($collection->values() as $index=>$hist){
//stuff here
$next =$collection->get(++$index) ;
}
刀片语法显然有点不同,但希望能说明这个想法