While paginate and simplePaginate create queries using the SQL "offset" clause, cursor pagination works by constructing "where" clauses that compare the values of the ordered columns contained in the query, providing the most efficient database performance available amongst all of Laravel's pagination methods. This method of pagination is particularly well-suited for large data-sets and "infinite" scrolling user interfaces.
<?php $postTemp = Post::cursorPaginate(12, ['*'], 'cursor', Cursor::fromEncoded($nextCursor)); $nextCursor = $postTemp->nextCursor()->encode();
Full Code Example
The full code example will be as follows.
<?php $posts = new Collection(); $nextCursor = null; hasMorePages = false; $postTemp = Post::cursorPaginate(12, ['*'], 'cursor', Cursor::fromEncoded($nextCursor)); $posts->push(...$postTemp->items()); if ($hasMorePages = $postTemp->hasMorePages()) { $nextCursor = $postTemp->nextCursor()->encode(); }
Leave a reply