Search

Laravel 5 Collections: Reversing the Order of Collection Elements With reverse

April 22, 2018 —John Koster

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 reverse method is similar in behavior to PHP's array_reverse function.

#Signature

1public function reverse();

#Example Use

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

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 'first', 'second', 'third'
6]);
7 
8$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)
2 protected 'items' =>
3 array
4 0 => string 'third'
5 1 => string 'second'
6 2 => string 'first'

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.