title: Codeigniter 4 pagination on function in model
body: <p>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:</p>
  <p>My model:</p>
  <pre><code>&lt;?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-&gt;table('categories');
              $query   = $builder-&gt;get();
              $results = $query-&gt;getResultArray();
          } else {
              $builder = $db-&gt;table('categories');
              $query   = $builder-&gt;where('categoryID', $categoryID);
              $query   = $builder-&gt;get();
              $results   = $query-&gt;getRow();
          }
  
          return $results;
      }
  }
  </code></pre>
  <p>In my controller, i try to paginate the results of the &quot;getCategories&quot;-function from the model:</p>
  <pre><code>&lt;?php
  
  namespace App\Controllers;
  
  use App\Models\CategoriesModel;
  use App\Models\PodcastsModel;
  
  class Categories extends BaseController
  {
      public function index($page = 1)
      {
          $this-&gt;request-&gt;setLocale(session()-&gt;get('locale'));
  
          //Helper laden
          helper(['form', 'template', 'userrights']);
          $data['template'] = getTemplate();
  
         
          $model = new CategoriesModel;
  
          $data['categories'] = $model-&gt;getCategories()-&gt;paginate(5, 'test', $page, 2);
  
          $data['pager'] = $model-&gt;pager;
  
          //Recht &quot;13&quot; (Benutzergruppen) prüfen
          if ($data['userrights'][13] == 0) {
              return redirect()-&gt;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');
      }
  }
  </code></pre>
  <p>So the line</p>
  <pre><code>$data['categories'] = $model-&gt;getCategories()-&gt;paginate(5, 'test', $page, 2);
  </code></pre>
  <p>will cause the following error:</p>
  <blockquote>
  <p>Error
  Call to a member function paginate() on array</p>
  </blockquote>
  <p>When i use</p>
  <pre><code>$data['categories'] = $model-&gt;paginate(5, 'test', $page, 2);
  </code></pre>
  <p>it works just fine. But it paginates ALL results from the categories table, as declared in the model.</p>
  <p>But as i want to paginate the results from the function, depending on variables i pass to the model,</p>
  <p>Is there a way to use the pagination class for model-functions?</p>