Welcome Guest, Not a member yet? Register   Sign In
Pagination with Custom model

Add pager in your controller :
PHP Code:
<?php namespace App\Controllers;


Annunci extends BaseController{

  public function index() {
     $model = new CustomModel($db);
     $annunci $model->annunci();
$annunci $model->pager;

     return view('cet',$annunci);



And change your model from : 
PHP Code:
public function annunci(){
  return $this->db->table('annunci')

To : 
PHP Code:
public function annunci(){
  return $this->table('annunci')
paginate(4'annunci'); // 4 for limiting data


@MuhAnhar No, he’s not extending the Model class. His model doesn’t have a pager object and no paginate function.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/

(01-07-2021, 08:19 AM)includebeer Wrote: Ok, it wasn’t clear your model do not extend CodeIgniter’s model. We just see one function.

If you really don’t want to extend the Model class you can take a look at how the pagination is implemented in this class: https://github.com/codeigniter4/CodeIgni....php#L1165

You will need to use the Pager service. You can’t call findAll() because it’s part of the Model class, so you will need to call the limit() function of the query builder to set the limit and offset for the select query.

PHP Code:
     * Works with $this->builder to get the Compiled Select to operate on.
     * Expects a GET variable (?page=2) that specifies the page of results
     * to display.
     * @param integer $perPage
     * @param string  $group   Will be used by the pagination library
     *                         to identify a unique pagination set.
     * @param integer $page    Optional page number (useful when the page number is provided in different way)
     * @param integer $segment Optional URI segment number (if page number is provided by URI segment)
     * @return array|null
public function paginate(int $perPage nullstring $group 'default'int $page nullint $segment 0)
$pager = \Config\Services::pager(nullnullfalse);

        if (

$page $page >= $page $pager->getCurrentPage($group);

$total $this->countAllResults(false);

// Store it in the Pager library so it can be
        // paginated in the views.
$this->pager $pager->store($group$page$perPage$total$segment);
$perPage     $this->pager->getPerPage($group);
$offset      = ($page 1) * $perPage;


Sorry to get back to you so late but wanted to thank you for your reply. I will look into it and try to solve the matter. I guess essentially I need to redefine my own paginate function if I understand you correctly

If he is not using the CodeIgniter Model then he needs to load the Pagination Library.

PHP Code:
$pager = \Config\Services::pager(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Theme © iAndrew 2016 - Forum software by © MyBB