@@ -282,14 +282,49 @@ public function sole($columns = ['*'])
282282 }
283283
284284 /**
285- * Pass the query to a given callback .
285+ * Paginate the given query using a cursor paginator .
286286 *
287- * @param callable $callback
288- * @return $this
287+ * @param int $perPage
288+ * @param array $columns
289+ * @param string $cursorName
290+ * @param string|null $cursor
291+ * @return \Illuminate\Contracts\Pagination\CursorPaginator
289292 */
290- public function tap ( $ callback )
293+ protected function paginateUsingCursor ( $ perPage , $ columns = [ ' * ' ], $ cursorName = ' cursor ' , $ cursor = null )
291294 {
292- return $ this ->when (true , $ callback );
295+ $ cursor = $ cursor ?: CursorPaginator::resolveCurrentCursor ($ cursorName );
296+
297+ $ orders = $ this ->ensureOrderForCursorPagination (! is_null ($ cursor ) && $ cursor ->pointsToPreviousItems ());
298+
299+ if (! is_null ($ cursor )) {
300+ $ addCursorConditions = function (self $ builder , $ previousColumn , $ i ) use (&$ addCursorConditions , $ cursor , $ orders ) {
301+ if (! is_null ($ previousColumn )) {
302+ $ builder ->where ($ previousColumn , '= ' , $ cursor ->parameter ($ previousColumn ));
303+ }
304+
305+ $ builder ->where (function (self $ builder ) use ($ addCursorConditions , $ cursor , $ orders , $ i ) {
306+ ['column ' => $ column , 'direction ' => $ direction ] = $ orders [$ i ];
307+
308+ $ builder ->where ($ column , $ direction === 'asc ' ? '> ' : '< ' , $ cursor ->parameter ($ column ));
309+
310+ if ($ i < $ orders ->count () - 1 ) {
311+ $ builder ->orWhere (function (self $ builder ) use ($ addCursorConditions , $ column , $ i ) {
312+ $ addCursorConditions ($ builder , $ column , $ i + 1 );
313+ });
314+ }
315+ });
316+ };
317+
318+ $ addCursorConditions ($ this , null , 0 );
319+ }
320+
321+ $ this ->limit ($ perPage + 1 );
322+
323+ return $ this ->cursorPaginator ($ this ->get ($ columns ), $ perPage , $ cursor , [
324+ 'path ' => Paginator::resolveCurrentPath (),
325+ 'cursorName ' => $ cursorName ,
326+ 'parameters ' => $ orders ->pluck ('column ' )->toArray (),
327+ ]);
293328 }
294329
295330 /**
@@ -340,4 +375,15 @@ protected function cursorPaginator($items, $perPage, $cursor, $options)
340375 'items ' , 'perPage ' , 'cursor ' , 'options '
341376 ));
342377 }
378+
379+ /**
380+ * Pass the query to a given callback.
381+ *
382+ * @param callable $callback
383+ * @return $this
384+ */
385+ public function tap ($ callback )
386+ {
387+ return $ this ->when (true , $ callback );
388+ }
343389}
0 commit comments