Skip to content

Commit 9e919b8

Browse files
author
Marius
committed
add-cursor-paginate
1 parent d47a871 commit 9e919b8

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,21 @@ Json Response:
209209
"column_name1",
210210
"column_name2"
211211
],
212-
"current_page": 1,
212+
"current_page": 1, // not present when cursor is present in request
213213
"data": [
214214
{
215215
"identifier":"value",
216216
"column_name":"value",
217217
...
218218
}
219219
],
220-
"from": 1,
221-
"last_page": 1, // not present when simplePaginate is true in controller or present on request
220+
"from": 1, // not present when cursor is present in request
221+
"last_page": 1, // not present when cursor is present in request or when simplePaginate is true in controller or present in request
222222
"per_page": 10,
223-
"to": 1,
224-
"total": 1, // not present when simplePaginate is true in controller or present on request
225-
"has_more_pages": bool
223+
"to": 1, // not present when cursor is present in request
224+
"total": 1, // not present when cursor is present in request or simplePaginate is true in controller or present in request
225+
"has_more_pages": bool,
226+
"cursor": "..." // present only when cursor is present in request
226227
}
227228

228229
and for application/xls: binary with contents from `data`
@@ -232,16 +233,19 @@ The reserved words / parameters that will be used as query params are:
232233
page,
233234
limit,
234235
simplePaginate
236+
cursor
235237

236238
Defaults:
237239

238240
page=1;
239241
limit=10;
240242
simplePaginate is false by default and only its presence is check in request, not its value
243+
cursor is not defined
241244

242245
Obs.
243246

244247
index_required_on_filtering key CAN'T be used for filtering.
248+
use ?cursor= for cursor pagination and ?simplePaginate=1 for simplePaginate. Use none of them for length aware paginator.
245249

246250
#### I.4 Update resource (or create)
247251
**PUT** /{resource}/{identifier}

src/Exports/ListResourceExcel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace MacropaySolutions\LaravelCrudWizard\Exports;
44

5-
use Illuminate\Pagination\AbstractPaginator;
65
use Illuminate\Database\Eloquent\Relations\Relation;
6+
use Illuminate\Pagination\CursorPaginator;
7+
use Illuminate\Pagination\LengthAwarePaginator;
8+
use Illuminate\Pagination\Paginator;
79
use Illuminate\Support\Collection;
810
use Illuminate\Support\Facades\Log;
911
use Illuminate\Support\Str;
@@ -24,7 +26,7 @@ class ListResourceExcel implements WithMultipleSheets
2426

2527
public function __construct(
2628
BaseModel $baseModel,
27-
AbstractPaginator $lap,
29+
LengthAwarePaginator | Paginator | CursorPaginator $lap,
2830
array $withRelations = [],
2931
?BaseModel $baseResourceModel = null
3032
) {

src/Http/Controllers/ResourceControllerTrait.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace MacropaySolutions\LaravelCrudWizard\Http\Controllers;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\ModelNotFoundException;
7+
use Illuminate\Database\Eloquent\Relations\Relation;
68
use Illuminate\Http\JsonResponse;
79
use Illuminate\Http\Request;
10+
use Illuminate\Pagination\CursorPaginator;
811
use Illuminate\Pagination\LengthAwarePaginator;
912
use Illuminate\Pagination\Paginator;
1013
use Illuminate\Support\Facades\Log;
@@ -166,13 +169,9 @@ protected function getFilteredRelations(array $relations, ?BaseModel $baseModel
166169
protected function handleList(array $allRequest, Request $request): Response
167170
{
168171
try {
169-
$paginator = $this->resourceService->list(
172+
$paginator = $this->getPaginator(
173+
$this->resourceService->list($allRequest),
170174
$allRequest
171-
)->{$this->simplePaginate ? 'simplePaginate' : 'paginate'}(
172-
max((int)($allRequest['limit'] ?? 10), 1),
173-
['*'],
174-
'page',
175-
\max((int)($allRequest['page'] ?? 1), 1)
176175
);
177176

178177
if ($request->header('Accept') === 'application/xls') {
@@ -225,10 +224,14 @@ protected function getEmptyPaginatedResponse(array $request): JsonResponse
225224
);
226225
}
227226

228-
protected function getJsonResponse(LengthAwarePaginator | Paginator $paginator, array $appends = []): JsonResponse
229-
{
227+
protected function getJsonResponse(
228+
LengthAwarePaginator | Paginator | CursorPaginator $paginator,
229+
array $appends = []
230+
): JsonResponse {
230231
return GeneralHelper::app(JsonResponse::class, [
231-
'data' => \array_merge([
232+
'data' => \array_merge($paginator instanceof CursorPaginator ? [
233+
'cursor' => $paginator->nextCursor()->encode(),
234+
] : [], [
232235
'has_more_pages' => $paginator->hasMorePages(),
233236
], $appends, GeneralHelper::filterDataByKeys(
234237
$paginator->toArray(),
@@ -275,7 +278,7 @@ protected function getDownloadHeaders(): array
275278
return [];
276279
}
277280

278-
protected function downloadXLS(LengthAwarePaginator | Paginator $paginator): Response
281+
protected function downloadXLS(LengthAwarePaginator | Paginator | CursorPaginator $paginator): Response
279282
{
280283
/** @var ListResourceExcel $exporter */
281284
$exporter = GeneralHelper::app(ListResourceExcel::class, [
@@ -292,6 +295,30 @@ protected function downloadXLS(LengthAwarePaginator | Paginator $paginator): Res
292295

293296
protected function setSimplePaginate(array $allRequest): void
294297
{
295-
$this->simplePaginate = isset($allRequest['simplePaginate']) ? true : $this->simplePaginate;
298+
$this->simplePaginate = isset($allRequest['simplePaginate'])
299+
|| isset($allRequest['cursor']) ? true : $this->simplePaginate;
300+
}
301+
/**
302+
* @throws \Throwable
303+
*/
304+
protected function getPaginator(
305+
Builder | Relation $builder,
306+
array $allRequest
307+
): LengthAwarePaginator | Paginator | CursorPaginator {
308+
if (isset($allRequest['cursor'])) {
309+
return $builder->cursorPaginate(
310+
max((int)($allRequest['limit'] ?? 10), 1),
311+
['*'],
312+
'cursor',
313+
$allRequest['cursor']
314+
);
315+
}
316+
317+
return $builder->{$this->simplePaginate ? 'simplePaginate' : 'paginate'}(
318+
max((int)($allRequest['limit'] ?? 10), 1),
319+
['*'],
320+
'page',
321+
\max((int)($allRequest['page'] ?? 1), 1)
322+
);
296323
}
297324
}

0 commit comments

Comments
 (0)