12345678910111213141516171819202122232425262728293031323334353637 |
- <?php
- namespace App\Common;
- class GetData
- {
- /**
- * @param object $query
- * @return \Generator
- */
- public static function generator(object $query, $limit = 500)
- {
- $idMin = 0;
- $idMax = (clone $query)->max('id');
- while ($idMin < $idMax) {
- $list = (clone $query)
- ->where('id', '>', $idMin)
- ->where('id', '<=', $idMax)
- ->where('enable', 1)
- ->orderBy('id', 'ASC')
- ->limit($limit)
- ->get();
- $count = $list->count();
- if ($count == 0) break;
- yield $list;
- if ($count < $limit) break;
- $idMin = $list->max('id');
- }
- }
- }
|