diff --git a/src/Queries/BaseQuery.php b/src/Queries/BaseQuery.php index e05a275..94ac66c 100644 --- a/src/Queries/BaseQuery.php +++ b/src/Queries/BaseQuery.php @@ -128,28 +128,10 @@ public function inertia(): QueryResponse { $this->loadRequest(); - $response = QueryResponse::make( + return QueryResponse::make( original: $this->paginate(), defaultSort: $this->defaultSort ); - if ($this->noPaginate) { - $url = $this->request->url(); - $perPage = count($response->data); - - $response->current_page = 1; - $response->first_page_url = $url; - $response->from = 1; - $response->last_page = 1; - $response->last_page_url = $url; - $response->next_page_url = null; - $response->path = $url; - $response->per_page = $perPage; - $response->prev_page_url = null; - $response->to = $perPage; - $response->total = $perPage; - } - - return $response; } /** diff --git a/src/Queries/QueryResponse.php b/src/Queries/QueryResponse.php index e59615d..7622816 100644 --- a/src/Queries/QueryResponse.php +++ b/src/Queries/QueryResponse.php @@ -41,23 +41,49 @@ public static function make(LengthAwarePaginator|Collection $original, string $d $array = $original->toArray(); - return new self( + $url = request()->url(); + $items = $original->items(); + $perPage = count($items); + + $self = new self( sort: request()->get('sort', $defaultSort), filter: request()->get('filter'), - data: $original->items(), + data: $items, current_page: $original->currentPage(), - first_page_url: $array['first_page_url'] ?? null, - from: $original->firstItem(), + first_page_url: $array['first_page_url'] ?? $url, + from: $original->firstItem() ?? 1, last_page: $original->lastPage(), last_page_url: $original->url($original->lastPage()), links: QueryResponseLink::toArray($array['links'] ?? []), next_page_url: $original->nextPageUrl(), - path: $original->path(), + path: $original->path() ?? $url, per_page: $original->perPage(), prev_page_url: $original->previousPageUrl(), - to: $original->lastItem(), + to: $original->lastItem() ?? $perPage, total: $original->total(), ); + + if (! $self->current_page) { + $self->current_page = 1; + } + + if (! $self->last_page) { + $self->last_page = 1; + } + + if (! $self->last_page_url) { + $self->last_page_url = $url; + } + + if (! $self->per_page) { + $self->per_page = $perPage; + } + + if (! $self->total) { + $self->total = $perPage; + } + + return $self; } }