$collection->pagination() returns null

Pretty sure I miss somthing but why is $collection->pagination() null?

Kirby::plugin('my/plugin', [
    'api'          => [
        'data'   => [
            'pages' => function ($query, $page) {
                $collection = new Collection();
                $results = site()->search($query)->paginate(5, $page);

                foreach ($results as $result) {
                    $collection->append([
                        'title' => $result->title()->value(),
                    ]);
                }

                return [
                    'data'       => $collection->data(),
                    'pagination' => $collection->pagination(),
                ];
            },
        ],
        'routes' => [
            [
                'pattern' => 'enhanced-toolbar-link-dialog/pages',
                'method'  => 'get',
                'action'  => function () {
                    $page = get('page');
                    $query = get('query');

                    if (empty($query)) {
                        $query = '*';
                    }

                    $results = $this->pages($query, $page);

                    return [
                        'pagination' => $results['pagination'],
                        'data'       => $results['data']
                    ];
                },
            ],
        ],
    ],
]);

Sorry for the messy code. Just fiddling to understand collections :smiley:

As far as I understand site()->search($query)->paginate(5, $page); correctly the paginate method should create a pagination instance.

$this->pagination = Pagination::for($this, ...$arguments);

You paginate $results, but call the pagination() method on $collection which is not paginated…

Indeed… I changed the code slightl: pagination is now an empty JSON object. ‘{}’

The other objects are doing fine.

Kirby::plugin('my/plugin', [
    'api' => [
        'data'   => [
            'pages' => function ($query, $page) {

                $collection = new Collection();
                $results = site()->search($query);
                
                foreach ($results as $result) {
                    $collection->append([
                        'title' => $result->title()->value(),
                    ]);
                }

                return [
                    'total'      => $results->count(),
                    'data'       => $collection->paginate(5, ['page' => $page])->data(),
                    'pagination' => $collection->pagination(),
                ];
            },
        ],
        'routes' => [
            [
                'pattern' => 'enhanced-toolbar-link-dialog/pages',
                'method'  => 'get',
                'action'  => function () {
                    $page = get('page');
                    $query = get('query');
                    if (empty($query)) {
                        $query = '*';
                    }
                    $results = $this->pages($query, $page);

                    return [
                        'total'      => $results['total'],
                        'pagination' => $results['pagination'],
                        'data'       => $results['data'],
                    ];
                },
            ],
        ],
    ],

Response

total 21
pagination {}
data […]

I guess the issue is that I create a new collection…

That shouldn’t make a difference, why shouldn’t it be possible to paginate a new collection?