From 2a1a17ca5f11645db3e0cf8dfce5e135fe831d71 Mon Sep 17 00:00:00 2001 From: Henning Zimmermann Date: Wed, 3 Apr 2024 09:47:49 +0200 Subject: [PATCH] - disable the pagination while exporting a collection --- src/ProcessDataSource.php | 14 ++++++++++---- src/Traits/WithExport.php | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ProcessDataSource.php b/src/ProcessDataSource.php index a7e50c58..5fe437b8 100644 --- a/src/ProcessDataSource.php +++ b/src/ProcessDataSource.php @@ -41,12 +41,12 @@ public function queryLog(): array /** * @throws Throwable */ - public function get(): Paginator|LengthAwarePaginator|\Illuminate\Pagination\LengthAwarePaginator|BaseCollection + public function get(bool $isExport = false): Paginator|LengthAwarePaginator|\Illuminate\Pagination\LengthAwarePaginator|BaseCollection { $datasource = $this->prepareDataSource(); if ($datasource instanceof BaseCollection) { - return $this->processCollection($datasource); + return $this->processCollection($datasource, $isExport); } $this->setCurrentTable($datasource); @@ -78,7 +78,7 @@ public function prepareDataSource(): EloquentBuilder|BaseCollection|Collection|Q /** * @throws \Exception */ - private function processCollection(mixed $datasource): \Illuminate\Pagination\LengthAwarePaginator|BaseCollection + private function processCollection(mixed $datasource, bool $isExport = false): \Illuminate\Pagination\LengthAwarePaginator|BaseCollection { /** @var BaseCollection $datasource */ cache()->forget($this->component->getId()); @@ -92,7 +92,13 @@ private function processCollection(mixed $datasource): \Illuminate\Pagination\Le if ($results->count()) { $this->component->filtered = $results->pluck($this->component->primaryKey)->toArray(); - $paginated = Collection::paginate($results, intval(data_get($this->component->setUp, 'footer.perPage'))); + if ($isExport) { + $perPage = $this->component->total; + } else { + $perPage = intval(data_get($this->component->setUp, 'footer.perPage')); + } + + $paginated = Collection::paginate($results, $perPage); $results = $paginated->setCollection($this->transform($paginated->getCollection(), $this->component)); }; diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index 2d8211d6..b72feb6d 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -170,7 +170,7 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup if ($processDataSource->isCollection) { if ($inClause) { - $results = $processDataSource->get()->whereIn($this->primaryKey, $inClause); + $results = $processDataSource->get(true)->whereIn($this->primaryKey, $inClause); return $processDataSource->transform($results, $this); }