优惠券小程序

GetData.php 742B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Common;
  3. class GetData
  4. {
  5. /**
  6. * @param object $query
  7. * @return \Generator
  8. */
  9. public static function generator(object $query, $limit = 500)
  10. {
  11. $idMin = 0;
  12. $idMax = (clone $query)->max('id');
  13. while ($idMin < $idMax) {
  14. $list = (clone $query)
  15. ->where('id', '>', $idMin)
  16. ->where('id', '<=', $idMax)
  17. ->where('enable', 1)
  18. ->orderBy('id', 'ASC')
  19. ->limit($limit)
  20. ->get();
  21. $count = $list->count();
  22. if ($count == 0) break;
  23. yield $list;
  24. if ($count < $limit) break;
  25. $idMin = $list->max('id');
  26. }
  27. }
  28. }