diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index d799f8cf..0c1aae51 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Query\Expression; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; @@ -1126,8 +1127,19 @@ public function getResultsProperty() { $this->row = 1; + $perPage = ($this->getQuery()->limit && ($this->getQuery()->limit < $this->perPage)) ? $this->getQuery()->limit : $this->perPage; + $paginatedQuery = $this->getQuery()->paginate($perPage); + $total = ($this->getQuery()->limit) ?: $paginatedQuery->total(); + $paginatedQuery = new LengthAwarePaginator( + $paginatedQuery->items(), + $paginatedQuery->total() < $total ? $paginatedQuery->total() : $total, + $this->perPage, + $paginatedQuery->currentPage(), + $paginatedQuery->getOptions() + ); + return $this->mapCallbacks( - $this->getQuery()->paginate($this->perPage) + $paginatedQuery ); } @@ -1704,7 +1716,7 @@ public function getQuery($export = false) public function checkboxQuery() { - return $this->query->reorder()->get()->map(function ($row) { + return $this->query->get()->map(function ($row) { return (string) $row->checkbox_attribute; }); } @@ -1723,7 +1735,7 @@ public function toggleSelectAll() $this->visibleSelected = $visible_checkboxes; } } else { - if (count($this->selected) === $this->getQuery()->getCountForPagination()) { + if (count($this->selected) === ($this->getQuery()->limit ?: $this->getQuery()->getCountForPagination())) { $this->selected = []; } else { $this->selected = $this->checkboxQuery()->values()->toArray();