Skip to content

Commit

Permalink
fix eachById on hasManyThrough relation (#47479)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancalara authored Jun 18, 2023
1 parent a0e03b0 commit 68ef112
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit 68ef112

Please sign in to comment.