John Davidson

php - Laravel cursor pagination loop inside console command

0 comments
Message:


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 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us