From ba80e77c6b1c5f9f95d91c9a530abbafc5399697 Mon Sep 17 00:00:00 2001 From: Matus Rohal Date: Wed, 5 Mar 2025 17:41:10 +0100 Subject: [PATCH] Refactor pagination logic to ensure consistent total item count handling --- src/Traits/WithData.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index c8c4f4a75..4cc1257d5 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -107,17 +107,23 @@ protected function executeQuery(): Collection|CursorPaginator|Paginator|LengthAw $this->paginationTotalItemCount = $this->getBuilder()->count(); return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? $this->paginationTotalItemCount : $this->getPerPage(), ['*'], $this->getComputedPageName()); - } else { - $this->paginationTotalItemCount = -1; - - return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? 10 : $this->getPerPage(), ['*'], $this->getComputedPageName()); } + $this->paginationTotalItemCount = -1; + + return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? 10 : $this->getPerPage(), ['*'], $this->getComputedPageName()); + } elseif ($this->isPaginationMethod('cursor')) { - $this->paginationTotalItemCount = $this->getBuilder()->count(); + if ($this->getShouldRetrieveTotalItemCount()) { + $this->paginationTotalItemCount = $this->getBuilder()->count(); + + return $this->getBuilder()->cursorPaginate($this->getPerPage() === -1 ? $this->paginationTotalItemCount : $this->getPerPage(), ['*'], $this->getComputedPageName()); + } + + $this->paginationTotalItemCount = -1; - return $this->getBuilder()->cursorPaginate($this->getPerPage() === -1 ? $this->paginationTotalItemCount : $this->getPerPage(), ['*'], $this->getComputedPageName()); + return $this->getBuilder()->cursorPaginate($this->getPerPage() === -1 ? 10 : $this->getPerPage(), ['*'], $this->getComputedPageName()); } else { throw new DataTableConfigurationException('Pagination method must be either simple, standard or cursor'); }