Skip to content

Commit 68ef112

Browse files
fix eachById on hasManyThrough relation (#47479)
1 parent a0e03b0 commit 68ef112

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,24 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
566566
return $this->prepareQueryBuilder()->chunkById($count, $callback, $column, $alias);
567567
}
568568

569+
/**
570+
* Execute a callback over each item while chunking by ID.
571+
*
572+
* @param callable $callback
573+
* @param int $count
574+
* @param string|null $column
575+
* @param string|null $alias
576+
* @return bool
577+
*/
578+
public function eachById(callable $callback, $count = 1000, $column = null, $alias = null)
579+
{
580+
$column = $column ?? $this->getRelated()->getQualifiedKeyName();
581+
582+
$alias = $alias ?? $this->getRelated()->getKeyName();
583+
584+
return $this->prepareQueryBuilder()->eachById($callback, $count, $column, $alias);
585+
}
586+
569587
/**
570588
* Get a generator for the given query.
571589
*

tests/Database/DatabaseEloquentHasManyThroughIntegrationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,26 @@ public function testEachReturnsCorrectModels()
407407
});
408408
}
409409

410+
public function testEachByIdReturnsCorrectModels()
411+
{
412+
$this->seedData();
413+
$this->seedDataExtended();
414+
$country = HasManyThroughTestCountry::find(2);
415+
416+
$country->posts()->eachById(function ($post) {
417+
$this->assertEquals([
418+
'id',
419+
'user_id',
420+
'title',
421+
'body',
422+
'email',
423+
'created_at',
424+
'updated_at',
425+
'laravel_through_key',
426+
], array_keys($post->getAttributes()));
427+
});
428+
}
429+
410430
public function testLazyReturnsCorrectModels()
411431
{
412432
$this->seedData();

0 commit comments

Comments
 (0)