Laravel Collection Public API: reverse

November 30, 2016 —John Koster

reverse

The reverse method is used to reverse the order of items in a collection. The reverse method returns a new Collection instance. The reverse method does not preserve numerical keys but will preserve non-numerical keys.

The following code example highlights the usage of the reverse method:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection instance.
6$collection = new Collection([
7 'first', 'second', 'third'
8]);
9 
10$reversed = $collection->reverse();

The $reversed variable would now hold an instance of Collection and would contain a value similar to the following output:

1object(Illuminate\Support\Collection)[134]
2 protected 'items' =>
3 array (size=3)
4 0 => string 'third' (length=5)
5 1 => string 'second' (length=6)
6 2 => string 'first' (length=5)

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.