Collection::reverse
Flips the order of a collection's items while keeping each item's original key.
Collection::reverse(): staticReturn value
staticA new collection with the items in the opposite order and their original keys intact.
It calls array_reverse() with key preservation turned on, so a list reversed from five items comes back keyed 4, 3, 2, 1, 0 rather than 0..4. In a Blade @foreach that difference is invisible; in json_encode() it is not, because non-sequential keys serialize as a JSON object. Chain values() whenever the result crosses a boundary that expects a list.
Reach for it when the desired order is already encoded in the collection and only needs flipping — the newest-first feed you want to render oldest-first, a breadcrumb trail walked up from a leaf, a stack of undo steps. If you actually want the items ordered by some property, use sort(), sortBy() or sortByDesc(); reversing a sorted collection gives the same result but does twice the work and reads as a workaround.
Associative collections
String keys are preserved too, so reversing a keyed map returns the same pairs in the opposite insertion order. That is genuinely useful for ordered configuration or a middleware pipeline that needs unwinding, and it is a no-op for anything you then look up by key.
The original collection is untouched — a new instance is returned.
Example
When to use it
- Render a newest-first chat history oldest-first without re-querying.
- Unwind a middleware or decorator pipeline that was built top-down.
- Walk a breadcrumb trail collected from leaf to root back out to root to leaf.
Frequently asked questions
Does reverse renumber the array keys?
No — it preserves them, so a five-item list comes back keyed 4, 3, 2, 1, 0. Chain values() to reindex from zero, which matters for json_encode and for anything that assumes a contiguous list.
What is the difference between reverse and sortDesc?
reverse flips the order the collection is already in, while sortDesc orders the items by their values from high to low. They only agree when the collection was already sorted ascending.
Does reverse modify the original collection?
No, it returns a new collection and leaves the original untouched, like every other non-destructive collection method. Assign the result or keep chaining.
More examples
Why the keys matter when you serialize
The first line prints {"2":"oldest","1":"older","0":"newest"} because the keys are no longer sequential; the second prints ["oldest","older","newest"]. Note the last two lines: first() returns oldest (position), while indexing key 0 returns newest (identity). Confusing those is the usual bug.
<?php
$feed = collect(['newest', 'older', 'oldest']);
$reversed = $feed->reverse();
echo json_encode($reversed->all()), PHP_EOL;
echo json_encode($reversed->values()->all()), PHP_EOL;
echo $reversed->first(), PHP_EOL;
echo $reversed->all()[0], PHP_EOL;Reverse versus sortDesc
reverse() prints [150, 100, 200] — the same list backwards, not sorted. sortDesc() prints [200, 150, 100]. Reverse only gives you a descending order when the collection was already ascending.
<?php
$prices = collect([200, 100, 150]);
// Flips the order it is already in.
print_r($prices->reverse()->values()->all());
// Actually orders by value, high to low.
print_r($prices->sortDesc()->values()->all());In an application: replaying a chat thread
The query takes the 50 most recent rows, which requires descending order, and reverse() puts them back into reading order without a second query. values() is chained because the view paginates by index and an Eloquent collection would otherwise keep keys 49 down to 0.
<?php
namespace App\Http\Controllers;
use App\Models\Message;
class ThreadController extends Controller
{
public function show(int $threadId)
{
// Newest 50 from the database, then flipped for display.
$messages = Message::where('thread_id', $threadId)
->latest()
->limit(50)
->get()
->reverse()
->values();
return view('thread', ['messages' => $messages]);
}
}
Discussion