How can I loop through all the pages of a cursor pagination inside a Laravel console command?
$paginator = User::query()->orderBy('id')->cursorPaginate(2);
while ($paginator->hasPages()) {
foreach ($paginator->items() as $item) {
$this->info($item->id);
}
$paginator = $paginator->nextCursor();
}
I am stuck in looping, please help.
My Answer:
>orderBy('id')->cursorPaginate(100);
foreach ($paginator as $user) {
// Do something with each user
}
while ($paginator->hasMorePages()) {
$paginator = User::query()->orderBy('id')->cursorPaginate(100, ['cursor' => $paginator->nextCursor()]);
foreach ($paginator as $user) {
// Do something with each user
}
}
Rate this post
5 of 5 based on 6320 votesComments