diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index a61570ff1257..40f3cb48b9f7 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -391,12 +391,17 @@ public function chunk($count, callable $callback) * @param int $count * @param callable $callback * @param string $column + * @param string $alias Alias of the ID column if there are multiple columns with the same name. The alias must be defined in a select statement. * @return bool */ - public function chunkById($count, callable $callback, $column = 'id') + public function chunkById($count, callable $callback, $column = null, $alias = null) { $lastId = null; + $column = is_null($column) ? $this->getModel()->getKeyName() : null; + + $alias = is_null($alias) ? $column : $alias; + $results = $this->forPageAfterId($count, 0, $column)->get(); while (! $results->isEmpty()) { @@ -404,7 +409,7 @@ public function chunkById($count, callable $callback, $column = 'id') return false; } - $lastId = $results->last()->{$column}; + $lastId = $results->last()->{$alias}; $results = $this->forPageAfterId($count, $lastId, $column)->get(); }