Skip to content

Commit

Permalink
[5.2] Support column aliases in chunkById
Browse files Browse the repository at this point in the history
References laravel#14711
  • Loading branch information
Riesjart2 committed Aug 23, 2016
1 parent 65d0008 commit 11f4856
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,25 @@ 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()) {
if (call_user_func($callback, $results) === false) {
return false;
}

$lastId = $results->last()->{$column};
$lastId = $results->last()->{$alias};

$results = $this->forPageAfterId($count, $lastId, $column)->get();
}
Expand Down

0 comments on commit 11f4856

Please sign in to comment.