sam*_*les 6 php laravel laravel-collection
我循环浏览一个Laravel集合,按created_at
时间排序.每个项目的开始日期是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
Run Code Online (Sandbox Code Playgroud)
正如您在此处所看到的,结束日期与开始日期相同,但我需要它作为下一个项目的开始日期.我以为会有一个帮手,比如last()
我可以打电话的收藏品next()
或其他任何东西.由于没有数字键,我不能只添加一个并抓住它.该项目按日期排序,唯一的ID类型字段是数据库中的字段,它是随机ID,如1434或1356.
关于如何获取下一个项目的start_date的任何想法,并检查我们是否在最后一项?我查看了PHP的next
功能,但我不认为它能满足我的需求.
非常感谢
您必须先获得Iterator:
$collection = $collection->getIterator();
$current = current($collection);
$next = next($collection);
Run Code Online (Sandbox Code Playgroud)
该集合将由递增索引作为键,因此您应该能够执行以下操作...
$collection = $statushistory->where('itemid', $timelineitemid);
foreach ($collection->values() as $index=>$hist){
//stuff here
$next =$collection->get(++$index) ;
}
Run Code Online (Sandbox Code Playgroud)
Blade 语法显然有点不同,但希望能说明这个想法