Last active
August 9, 2023 03:24
-
-
Save rootchips/0ae392be3f6fafd2a8893270a6cd28d9 to your computer and use it in GitHub Desktop.
PHP Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php | |
<?php | |
namespace App\Process; | |
use App\Models\Model; | |
class Generate | |
{ | |
public int $offset = 0; | |
public int $limit = 2000; | |
public int $total = 20000; | |
private function getData(): array | |
{ | |
$collection = Model::query() | |
->offset($this->offset) | |
->limit($this->limit) | |
->get(); | |
if ($collection->count()) { | |
$this->offset += $this->limit; | |
// or you want to do insert qu its up to you | |
return $collection->toArray(); | |
} | |
return []; | |
} | |
private function cursor(): \Generator | |
{ | |
while ($this->total > $this->offset) { | |
yield from $this->getData(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment