0

I have a Post model that can have many comments. When I'm listing posts in a feed - both posts and post comments are paginated. The problem is when I visit post feed page that isn't first - post comments aren't paginated starting from the first page. They are starting from the same page as posts.

enter image description here

Is there a way to always force Laravel to start related entity pagination from page 1?

Matko Đipalo
  • 1,676
  • 1
  • 11
  • 23

1 Answers1

2

It looks like the reason why it happens is that the both paginators use the same page query parameter value by default. See the code.

You can change this behavior by providing the third parameter of the paginate() method call to something like 'comments_page'.

$post->comments()->paginate(15, ['*'], 'comments_page');

Now when you want to see the first page of the comments you can pass the comments_page query parameter value into the query string.

http://localhost:8000/api/v1/feeds/all-posts?page=2&comments_page=1
tooleks
  • 557
  • 5
  • 17