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

Firemaps's avatar

Reverse pagination

I am building a reversed pagination from a one to many relationship 1 user has many photos Currently the ID's are coming out Page 1 = 10, 9, 8... Page 2 = 19, 18, 17

How can I make page 1 the most recent = 19, 18, 17... and page 2 = 10, 9, 8...?

In my blade file where the photos are being displayed I am looping through all of the users photos to display them

 @foreach($user->photos()->paginate(10)->reverse() as $photo)

and in the same blade file I am building the pagination list

            @if($user->photos->count() > 10)
                <!-- incl pagination -->
                {{ $photos->links() }}
                <hr>
            @endif

In my controller I am passing through the user and their photos Unfrotunately orderBy('created_at', 'desc'); or 'asc' doesn't work either.

    public function getProfile() {
        $user = Auth::user();
        $photos = Photo::orderBy('user_id', $user->id)->paginate(10);
        return view('user.profile', ['user' => $user, 'photos' => $photos]);
    }

Hope someone can help, Thanks

0 likes
1 reply
willvincent's avatar

Just do something like this:

public function getProfile() {
  $user = Auth::user();

  // Fetch paginated photos, for specified user, newest photos first:
  $photos = Photo::where('user_id', $user->id)->orderBy('created_at', 'DESC')->paginate(10);

  return view('user.profile', compact('user', 'photos'));
}
@foreach ($photos as $photo)
  <!-- display photo here -->
@endforeach 
{{ $photos->links() }}

Please or to participate in this conversation.