To reverse the order of elements in a collection using Illuminate/Collections, you can make use of the reverse()
method. Here is an example:
use Illuminate\Support\Collection;
$collection = collect([1, 2, 3, 4, 5]);
reverse()
method to reverse the order of elements:$reversedCollection = $collection->reverse();
$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.