Skip to content

Instantly share code, notes, and snippets.

@rootchips
Last active August 9, 2023 03:24
Show Gist options
  • Save rootchips/0ae392be3f6fafd2a8893270a6cd28d9 to your computer and use it in GitHub Desktop.
Save rootchips/0ae392be3f6fafd2a8893270a6cd28d9 to your computer and use it in GitHub Desktop.
PHP Generator
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