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

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 (
$segment)
        {
            
$pager->setSegment($segment);
        }

        
$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;

        return 
$this->findAll($perPage$offset);
    } 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply


Messages In This Thread
Pagination with Custom model - by AndreaL - 12-31-2020, 03:59 AM
RE: Pagination with Custom model - by InsiteFX - 12-31-2020, 12:27 PM
RE: Pagination with Custom model - by AndreaL - 01-04-2021, 03:03 AM
RE: Pagination with Custom model - by seunex - 01-04-2021, 04:01 AM
RE: Pagination with Custom model - by includebeer - 01-04-2021, 05:33 AM
RE: Pagination with Custom model - by AndreaL - 01-07-2021, 02:31 AM
RE: Pagination with Custom model - by rmcdahal - 01-04-2021, 06:29 AM
RE: Pagination with Custom model - by includebeer - 01-07-2021, 08:19 AM
RE: Pagination with Custom model - by AndreaL - 02-09-2021, 03:41 AM
RE: Pagination with Custom model - by MuhAnhar - 01-12-2021, 06:06 AM
RE: Pagination with Custom model - by includebeer - 01-12-2021, 03:46 PM
RE: Pagination with Custom model - by InsiteFX - 02-09-2021, 07:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB