# Pagination for Comments

**URL:** <https://discourse.cakephp.org/t/pagination-for-comments/9729>\
**Category:** Need Help\
**Created:** [September 21, 2021, 10:08pm UTC](https://discourse.cakephp.org/t/pagination-for-comments/9729 "2021-09-21T22:08:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [September 21, 2021, 10:08pm UTC](https://discourse.cakephp.org/t/pagination-for-comments/9729/1 "2021-09-21T22:08:59Z")

</div>

Hey, is it also possible to create a Pagination for Comments that belong to an Article?

right now they can be accessed like:

$article-\>comments

and i do:

\<?php foreach ($article-\>comments as $key =\> $comment) { ?\>

but if there are hundrets of Comments that will not work anymore…

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [September 21, 2021, 10:39pm UTC](https://discourse.cakephp.org/t/pagination-for-comments/9729/2 "2021-09-21T22:39:17Z")

</div>

> [@Pagination on related records CakePHP4](https://discourse.cakephp.org/t/pagination-on-related-records-cakephp4/9614):
>
> When baking an app, you get a view.php able to show the child records related to a parent record: The child records are not paginated like they are by default in the index.php when baking that template. How to change the baked controller to get the pagination into effect? My ‘natural’ thing to do is: public function view($id = null) { $group = $this-\>paginate($this-\>Groups-\>get($id, ['contain' =\> ['Individuals'], ])); $th…

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [September 25, 2021, 8:30pm UTC](https://discourse.cakephp.org/t/pagination-for-comments/9729/3 "2021-09-25T20:30:45Z")

</div>

how can i limit this pagination to 5 for example?

$comments = $this-\>paginate($this-\>Comments  
-\>find()  
-\>contain([‘Users’, ‘Users.Profiles’])  
-\>limit(‘5’)  
-\>where([‘article\_id =’ =\> $id]))

because this is not working.

here it is working: $this-\>set(‘articles’,$this-\>paginate($articles,[‘limit’=\>‘3’]));  
but its a different syntax…

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [September 26, 2021, 4:23am UTC](https://discourse.cakephp.org/t/pagination-for-comments/9729/4 "2021-09-26T04:23:20Z")

</div>

In your first example, you’re passing a query in to `paginate`, which is overriding the limit with its own default. In the second case, you’re explicitly telling `paginate` what the limit should be. So:

```auto
$query = $this->Comments
    ->find()
    ->contain([‘Users’, ‘Users.Profiles’])
    ->where([‘article_id =’ => $id]);
$comments = $this->paginate($query, ['limit' => 5]);

```

should work as expected. (You can do it all without the `$query` variable; I separated it to make it clearer what is doing what.)
