Search code examples
phpcodeignitermodelpaginationcodeigniter-4

Codeigniter 4 pagination on function in model


I'm currently stuck with the built-in pagination of CI4. I try to paginate the results of a function in my model, which does not work:

My model:

<?php

namespace App\Models;

use CodeIgniter\Model;

class CategoriesModel extends Model
{

    protected $table = 'categories';
    protected $allowedFields = [];
    protected $beforeInsert = ['beforeInsert'];
    protected $beforeUpdate = ['beforeUpdate'];

    //Kategorie(n) laden
    public function getCategories($categoryID = null)
    {
        $db = \Config\Database::connect();

        if (!$categoryID) {
            $builder = $db->table('categories');
            $query   = $builder->get();
            $results = $query->getResultArray();
        } else {
            $builder = $db->table('categories');
            $query   = $builder->where('categoryID', $categoryID);
            $query   = $builder->get();
            $results   = $query->getRow();
        }

        return $results;
    }
}

In my controller, i try to paginate the results of the "getCategories"-function from the model:

<?php

namespace App\Controllers;

use App\Models\CategoriesModel;
use App\Models\PodcastsModel;

class Categories extends BaseController
{
    public function index($page = 1)
    {
        $this->request->setLocale(session()->get('locale'));

        //Helper laden
        helper(['form', 'template', 'userrights']);
        $data['template'] = getTemplate();

       
        $model = new CategoriesModel;

        $data['categories'] = $model->getCategories()->paginate(5, 'test', $page, 2);

        $data['pager'] = $model->pager;

        //Recht "13" (Benutzergruppen) prüfen
        if ($data['userrights'][13] == 0) {
            return redirect()->to('/admin');
        }

        //Views aufbauen
        echo view($data['template'] . '/templates/header', $data);
        echo view($data['template'] . '/backend/navigation');
        echo view($data['template'] . '/templates/sidebar');
        echo view($data['template'] . '/backend/categories');
        echo view($data['template'] . '/templates/footer');
    }
}

So the line

$data['categories'] = $model->getCategories()->paginate(5, 'test', $page, 2);

will cause the following error:

Error Call to a member function paginate() on array

When i use

$data['categories'] = $model->paginate(5, 'test', $page, 2);

it works just fine. But it paginates ALL results from the categories table, as declared in the model.

But as i want to paginate the results from the function, depending on variables i pass to the model,

Is there a way to use the pagination class for model-functions?


Solution

  • You're not using the paginate library correctly. It must be used at the end of your query builder instead of a get() for example.

    Your model could return the paginate object.

    public function getCategories($nb_paginate, $categoryID = null)
        {
            $db = \Config\Database::connect();
    
            if (!$categoryID) {
                $results = $this->paginate($nb_paginate);
            } else {
                $results = $this->where('categoryID', $categoryID)->paginate($nb_paginate);
            }
    
            return $results;
        }
    

    And then catch it with your controller

    $data['categories'] = $model->getCategories(5);
    $data['pager'] = $model->pager;
    

    You might want to give a look at the doc aswell : https://codeigniter.com/user_guide/libraries/pagination.html