Skip to content

Commit 2f3a8f1

Browse files
mpywtaylorotwell
authored andcommitted
Fix: Override BelongsToMany::cursor() to hydrate pivot relations (#30580)
1 parent 2385358 commit 2f3a8f1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,22 @@ public function each(callable $callback, $count = 1000)
749749
});
750750
}
751751

752+
/**
753+
* Get a lazy collection for the given query.
754+
*
755+
* @return \Illuminate\Support\LazyCollection
756+
*/
757+
public function cursor()
758+
{
759+
$this->query->addSelect($this->shouldSelect());
760+
761+
return $this->query->cursor()->map(function ($model) {
762+
$this->hydratePivotRelation([$model]);
763+
764+
return $model;
765+
});
766+
}
767+
752768
/**
753769
* Hydrate the pivot table relationship on the models.
754770
*

tests/Database/DatabaseEloquentIntegrationTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,18 @@ public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverEachRe
767767
});
768768
}
769769

770+
public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverCursorRequest()
771+
{
772+
$user = EloquentTestUser::create(['email' => '[email protected]']);
773+
$friend = $user->friends()->create(['email' => '[email protected]']);
774+
775+
foreach (EloquentTestUser::first()->friends()->cursor() as $result) {
776+
$this->assertSame('[email protected]', $result->email);
777+
$this->assertEquals($user->id, $result->pivot->user_id);
778+
$this->assertEquals($friend->id, $result->pivot->friend_id);
779+
}
780+
}
781+
770782
public function testBasicHasManyEagerLoading()
771783
{
772784
$user = EloquentTestUser::create(['email' => '[email protected]']);

0 commit comments

Comments
 (0)