Skip to content

Commit

Permalink
Fix: Override BelongsToMany::cursor() to hydrate pivot relations (#30580
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mpyw authored and taylorotwell committed Nov 13, 2019
1 parent 2385358 commit 2f3a8f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,22 @@ public function each(callable $callback, $count = 1000)
});
}

/**
* Get a lazy collection for the given query.
*
* @return \Illuminate\Support\LazyCollection
*/
public function cursor()
{
$this->query->addSelect($this->shouldSelect());

return $this->query->cursor()->map(function ($model) {
$this->hydratePivotRelation([$model]);

return $model;
});
}

/**
* Hydrate the pivot table relationship on the models.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,18 @@ public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverEachRe
});
}

public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverCursorRequest()
{
$user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
$friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);

foreach (EloquentTestUser::first()->friends()->cursor() as $result) {
$this->assertSame('abigailotwell@gmail.com', $result->email);
$this->assertEquals($user->id, $result->pivot->user_id);
$this->assertEquals($friend->id, $result->pivot->friend_id);
}
}

public function testBasicHasManyEagerLoading()
{
$user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
Expand Down

0 comments on commit 2f3a8f1

Please sign in to comment.