Illuminate\Database\Eloquent\Builder::chunkById PHP Method

chunkById() public method

Chunk the results of a query by comparing numeric IDs.
public chunkById ( integer $count, callable $callback, string $column = 'id' ) : boolean
$count integer
$callback callable
$column string
return boolean
    public function chunkById($count, callable $callback, $column = 'id')
    {
        $lastId = 0;
        do {
            $results = $this->forPageAfterId($count, $lastId, $column)->get();
            $countResults = $results->count();
            if ($countResults == 0) {
                break;
            }
            if (call_user_func($callback, $results) === false) {
                return false;
            }
            $lastId = $results->last()->{$column};
        } while ($countResults == $count);
        return true;
    }

Usage Example

Example #1
0
 /**
  * Chunk the results of a query by comparing numeric IDs.
  *
  * @param int $count
  * @param callable $callback
  * @param string $column
  * @return bool 
  * @static 
  */
 public static function chunkById($count, $callback, $column = 'id')
 {
     return \Illuminate\Database\Eloquent\Builder::chunkById($count, $callback, $column);
 }