Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Support column aliases in chunkById #14978

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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