# CakePHP 4 and the pagination class - initialstate last page

**URL:** <https://discourse.cakephp.org/t/cakephp-4-and-the-pagination-class-initialstate-last-page/12094>\
**Category:** Need Help\
**Tags:** help\
**Created:** [July 1, 2024, 6:25pm UTC](https://discourse.cakephp.org/t/cakephp-4-and-the-pagination-class-initialstate-last-page/12094 "2024-07-01T18:25:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProDaniBest](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/prodanibest/32/2521_2.png) [@ProDaniBest](https://discourse.cakephp.org/u/ProDaniBest)\
**Post date:** [July 1, 2024, 6:25pm UTC](https://discourse.cakephp.org/t/cakephp-4-and-the-pagination-class-initialstate-last-page/12094/1 "2024-07-01T18:25:49Z")

</div>

I want to use the Pagination class for my Document entity. I have the following code:

```auto
public function index()
{
    $queryParam = $this->request->getQueryParams();
    $documentsQuery = $this->Documents
        ->find('search', array('search' => $queryParam))
        ->contain(array('Users'));
    $totalDocuments = $documentsQuery->count();
    $defaultLimit = 15;
    $currentPage = ceil($totalDocuments / $defaultLimit);
    $paginationSettings = array(
        'limit' => $defaultLimit,
        'page' => $currentPage,
    );
    $documents = $this->paginate($documentsQuery, $paginationSettings);
    $this->set(compact('documents'));
}

```

The goal is to always jump to the last page during the initial load. However, I can no longer navigate to the other pages. It seems like the settings are overriding the queries, which is very inconvenient. How do you handle this? Or have I made a conceptual mistake? What would it look like otherwise? It’s important that I always go to the last page in the view with the normal ASC sorting.

---

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [July 2, 2024, 6:25am UTC](https://discourse.cakephp.org/t/cakephp-4-and-the-pagination-class-initialstate-last-page/12094/2 "2024-07-02T06:25:44Z")

</div>

Why do you want to redirect in the last page?, You can sort it DESC order

---

<div class="post-metadata">

**Author:** ![ProDaniBest](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/prodanibest/32/2521_2.png) [@ProDaniBest](https://discourse.cakephp.org/u/ProDaniBest)\
**Post date:** [July 2, 2024, 6:55am UTC](https://discourse.cakephp.org/t/cakephp-4-and-the-pagination-class-initialstate-last-page/12094/3 "2024-07-02T06:55:26Z")

</div>

It is nicer and more pleasant if the entries start at 1 and are continued up to the last page. The last page should also have the last entries. Better 1 2 3 instead of 3 2 1.

I have now found a solution, but I fear that line 550 is a bug in the Pagination Helper.  
PaginationHelper.php:550

```auto
...
        if (!empty($options['page']) && $options['page'] === 1) {
            $options['page'] = null;
        }
...

```

At least it makes no sense to remove the page query from the first page. Solution: After overloading the method in a separate Paginator Helper, my method now works.

Now my index method looks like this:

```auto
	/**
	 * Pagination Default.
	 *
	 * @var array
	 */
	public $paginate = [
        'limit' => 15
    ];

	/**
	 * Index method
	 *
	 * @return \Cake\Http\Response|null|void Renders view
	 */
	public function index()
	{
		$queryParam = $this->request->getQueryParams();
		$documentsQuery = $this->Documents
			->find('search', array('search' => $queryParam))
			->contain(array('Users'));
		$totalDocuments = $documentsQuery->count();
		$defaultLimit = 15;
		$currentPage = 1;
		if (isset($queryParam['page'])) {
			$currentPage = (int)$queryParam['page'];
		} else {
			$currentPage = ceil($totalDocuments / $defaultLimit);
		}
		$paginationSettings = [
			'page' => $currentPage,
		];
		$documents = $this->paginate($documentsQuery, $paginationSettings);
		$this->set(compact('documents',));
	}

```

Problem: If you set page as a parameter, the Paginator Helper in its current form cannot set the first page with ?page=1 as a query. See above. If you remove the if, everything works again.
