Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephan-v's avatar

Reverse paginate collection

How can I reverse this collection? Simply using the reverse function does not seem to work here.

public function index()
    {
        // get all beers and paginate them
        $beers = Beer::paginate(9);

        return view('beers.index', compact('beers'));
    }
0 likes
3 replies
stephan-v's avatar
stephan-v
OP
Best Answer
Level 4

Nevermind, I figured it out.

$beers = Beer::orderBy('created_at', 'desc')->paginate(9);
2 likes
stephan-v's avatar

By the way I had my array set up as chunks since I am using bootstrap rows to display my items.

Like this:

@foreach($beers->chunk(3) as $chunk)
    // display data
@endforeach 

If I reversed it like your example it won't work. If i use the laravel reverse method on the array it would only reverse that chunk. So it would reverse the 3 items. I guess the solution I used is therefore a good solution?

Please or to participate in this conversation.