Rahul
3 years ago
6164
9
Eloquent

Best way to paginate relations in laravel

Posted 3 years ago by Rahul

Hi i'm building a websites which have posts and option to make comments to that posts. but i stuck into a problem, here is the problem with scenario -

  1. One post can have many comments.
  2. so i can simply defined that relationship in laravel and fetch with query like this : Post::with('comments')->paginate(10); with other conditions. But now-
  3. what if a post can have 100 or 1000 of comments . this approach is simply horrible for many reasons . that query will kill me.

So what i want -

  1. how can i paginate those comments by 10 or so . and give user facility to read more comments and when they click on that link then next 10 comments will show on the list for that post and so on.. this will increase performance and even prevent me to look ugly.

How can we do that in laravel .. please help. Your help will be much appreciated

Please sign in or create an account to participate in this conversation.