Illuminate\Database\Query\Builder::chunk PHP Method

chunk() public method

Chunk the results of the query.
public chunk ( integer $count, callable $callback ) : boolean
$count integer
$callback callable
return boolean
    public function chunk($count, callable $callback)
    {
        $this->enforceOrderBy();
        $page = 1;
        do {
            $results = $this->forPage($page, $count)->get();
            $countResults = $results->count();
            if ($countResults == 0) {
                break;
            }
            // On each chunk result set, we will pass them to the callback and then let the
            // developer take care of everything within the callback, which allows us to
            // keep the memory low for spinning through large result sets for working.
            if (call_user_func($callback, $results) === false) {
                return false;
            }
            $page++;
        } while ($countResults == $count);
        return true;
    }

Usage Example

 /**
  * @test
  */
 public function it_load_events_for_aggregate()
 {
     $aggregateRootId = new BarId('BarId');
     $this->db->table('events')->willReturn($this->queryBuilder);
     $this->queryBuilder->where('aggregate_root_id', 'BarId')->willReturn($this->queryBuilder);
     $this->queryBuilder->chunk(1000, Argument::any())->willReturn(true);
     $this->eventStore->load($aggregateRootId);
 }
All Usage Examples Of Illuminate\Database\Query\Builder::chunk