0

I have two models, User and Comment, Each User can have multiple Comment, I can call paginate(3) on User and get 3 User in output per page but I need same thing as User for comments in UserCollection. Is it possible to limit comments that comes by UserCollection by paginating them too?

mohammad
  • 7
  • 6

1 Answers1

0

Yes you can, you just have to add in the third parameter of pageName to the paginate method to differentiate between them.

Something like:

$users->paginate($perPage, ['*'], 'users')
$comments->paginate($perPage, ['*'], 'comments_'. $user_id)

https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Builder.html#method_paginate

Note: While you can do this, I personally would just fetch the first 3/5 comments and then have a button saying click to load more and then have a separate route that you call via an AJAX request to get them.

Ben Gooding
  • 884
  • 9
  • 18
  • can you say which method is better to use ? which one is standard ? using ajax or using paginate? – mohammad Sep 27 '21 at 11:39
  • For this instance I would use AJAX requests to fetch any extra comments because it is 10x easier and it'll have a lot more readable and scaleable code – Ben Gooding Sep 27 '21 at 13:33