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

[10.x] Fix eachById on HasManyThrough relation #47479

Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,24 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
return $this->prepareQueryBuilder()->chunkById($count, $callback, $column, $alias);
}

/**
* Execute a callback over each item while chunking by ID.
*
* @param callable $callback
* @param int $count
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function eachById(callable $callback, $count = 1000, $column = null, $alias = null)
{
$column = $column ?? $this->getRelated()->getQualifiedKeyName();

$alias = $alias ?? $this->getRelated()->getKeyName();

return $this->prepareQueryBuilder()->eachById($callback, $count, $column, $alias);
}

/**
* Get a generator for the given query.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,26 @@ public function testEachReturnsCorrectModels()
});
}

public function testEachByIdReturnsCorrectModels()
{
$this->seedData();
$this->seedDataExtended();
$country = HasManyThroughTestCountry::find(2);

$country->posts()->eachById(function ($post) {
$this->assertEquals([
'id',
'user_id',
'title',
'body',
'email',
'created_at',
'updated_at',
'laravel_through_key',
], array_keys($post->getAttributes()));
});
}

public function testLazyReturnsCorrectModels()
{
$this->seedData();
Expand Down