Reference next item in a Laravel collection
<p>I am looping through a Laravel collection sorted by <code>created_at</code> to make a timeline. The start date for each item is <code>created_at</code>, and the end date is the <code>created_at</code> of the following item. If it is the last item, the event just lasts for a set 30 days.</p>

<p>My current code is this:</p>

<pre><code>@foreach ($statushistory-&gt;where('itemid', $timelineitemid) as $hist)
    [ '{{$hist-&gt;newstatus}}', new Date({{$hist-&gt;created_at-&gt;format('Y')}}, {{$hist-&gt;created_at-&gt;format('n')-1}}, {{$hist-&gt;created_at-&gt;format('j')}}), new Date({{$hist-&gt;created_at-&gt;format('Y')}}, {{$hist-&gt;created_at-&gt;format('n')-1}}, {{$hist-&gt;created_at-&gt;format('j')}}) ],
@endforeach
</code></pre>

<p>As you can see here the end date is just the same as the start date, but I need it to be the start date of the next item. I thought there would be a  helper like <code>last()</code> for collections that I could just call <code>next()</code> or whatever. As there's no numeric key, I can't just add one and grab it. The item are sorted by date, and the only ID type field is the one from the database which is a random ID like 1434 or 1356.</p>

<p>Any ideas on how to get the next item's start_date, and check if we are at the last item? I looked at PHP's <code>next</code> function but I don't think it does what I need.</p>

<p>Many thanks</p>