How to reverse the order of elements in a collection in illuminate/collections?

To reverse the order of elements in a collection using Illuminate/Collections, you can make use of the reverse() method. Here is an example:

  1. Import the necessary classes:
use Illuminate\Support\Collection;
  1. Create a collection object with your desired elements:
$collection = collect([1, 2, 3, 4, 5]);
  1. Use the reverse() method to reverse the order of elements:
$reversedCollection = $collection->reverse();
  1. You can access the reversed collection and display its elements in the reversed order:
$reversedCollection->each(function ($item) { echo $item . "\n"; });

The output will be:

5 4 3 2 1

In this example, we created a collection with elements [1, 2, 3, 4, 5] and used the reverse() method to obtain a new collection with the elements in reversed order. Finally, we looped through the reversed collection and printed each element.