From 68ef1122ab0950ba45b541f276735e8e9e378b8e Mon Sep 17 00:00:00 2001 From: Cristian Calara Date: Sun, 18 Jun 2023 12:22:38 +0300 Subject: [PATCH] fix eachById on hasManyThrough relation (#47479) --- .../Eloquent/Relations/HasManyThrough.php | 18 +++++++++++++++++ ...eEloquentHasManyThroughIntegrationTest.php | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 1514274ce380..ac5037185793 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -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. * diff --git a/tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php b/tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php index c67379e85ec1..2b5535c5c808 100644 --- a/tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php +++ b/tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php @@ -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();