Skip to content

Instantly share code, notes, and snippets.

@jaygaha
Created May 15, 2023 04:58
Show Gist options
  • Save jaygaha/4592b1e403618387df6f81d4143f5b34 to your computer and use it in GitHub Desktop.
Save jaygaha/4592b1e403618387df6f81d4143f5b34 to your computer and use it in GitHub Desktop.
Cursor pagination will offer better performance if the "order by" columns are indexed. To go next or previous page we can define as.

How to Get 'Next' Cursor for Cursor Paginate In Laravel

use Illuminate\Pagination\Cursor;

...
$nextCursor = $request->has('cursor') ? $request->get('cursor') : null;

$comments = Comment::cursorPaginate(20, ['*'], 'cursor', Cursor::fromEncoded($nextCursor));

// to get pagination metadata
$nextCursor = $comments->nextCursor()->encode();
$nextPageUrl = $comments->nextPageUrl();
$prevCursor = $comments->previousCursor()->encode();
$prevPageUrl = $comments->previousPageUrl();
$path = $comments->path();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment