Skip to content

Commit

Permalink
- disable the pagination while exporting a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
marineusde committed Apr 3, 2024
1 parent 56127b7 commit 2a1a17c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/ProcessDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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));
};

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2a1a17c

Please sign in to comment.