2
2
3
3
namespace MacropaySolutions \LaravelCrudWizard \Http \Controllers ;
4
4
5
+ use Illuminate \Database \Eloquent \Builder ;
5
6
use Illuminate \Database \Eloquent \ModelNotFoundException ;
7
+ use Illuminate \Database \Eloquent \Relations \Relation ;
6
8
use Illuminate \Http \JsonResponse ;
7
9
use Illuminate \Http \Request ;
10
+ use Illuminate \Pagination \CursorPaginator ;
8
11
use Illuminate \Pagination \LengthAwarePaginator ;
9
12
use Illuminate \Pagination \Paginator ;
10
13
use Illuminate \Support \Facades \Log ;
@@ -166,13 +169,9 @@ protected function getFilteredRelations(array $relations, ?BaseModel $baseModel
166
169
protected function handleList (array $ allRequest , Request $ request ): Response
167
170
{
168
171
try {
169
- $ paginator = $ this ->resourceService ->list (
172
+ $ paginator = $ this ->getPaginator (
173
+ $ this ->resourceService ->list ($ allRequest ),
170
174
$ allRequest
171
- )->{$ this ->simplePaginate ? 'simplePaginate ' : 'paginate ' }(
172
- max ((int )($ allRequest ['limit ' ] ?? 10 ), 1 ),
173
- ['* ' ],
174
- 'page ' ,
175
- \max ((int )($ allRequest ['page ' ] ?? 1 ), 1 )
176
175
);
177
176
178
177
if ($ request ->header ('Accept ' ) === 'application/xls ' ) {
@@ -225,10 +224,14 @@ protected function getEmptyPaginatedResponse(array $request): JsonResponse
225
224
);
226
225
}
227
226
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 {
230
231
return GeneralHelper::app (JsonResponse::class, [
231
- 'data ' => \array_merge ([
232
+ 'data ' => \array_merge ($ paginator instanceof CursorPaginator ? [
233
+ 'cursor ' => $ paginator ->nextCursor ()->encode (),
234
+ ] : [], [
232
235
'has_more_pages ' => $ paginator ->hasMorePages (),
233
236
], $ appends , GeneralHelper::filterDataByKeys (
234
237
$ paginator ->toArray (),
@@ -275,7 +278,7 @@ protected function getDownloadHeaders(): array
275
278
return [];
276
279
}
277
280
278
- protected function downloadXLS (LengthAwarePaginator | Paginator $ paginator ): Response
281
+ protected function downloadXLS (LengthAwarePaginator | Paginator | CursorPaginator $ paginator ): Response
279
282
{
280
283
/** @var ListResourceExcel $exporter */
281
284
$ exporter = GeneralHelper::app (ListResourceExcel::class, [
@@ -292,6 +295,30 @@ protected function downloadXLS(LengthAwarePaginator | Paginator $paginator): Res
292
295
293
296
protected function setSimplePaginate (array $ allRequest ): void
294
297
{
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
+ );
296
323
}
297
324
}
0 commit comments