Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Eager load pivot relations #38157

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,10 +1013,17 @@ protected function hydratePivotRelation(array $models)
// To hydrate the pivot relationship, we will just gather the pivot attributes
// and create a new Pivot model, which is basically a dynamic model that we
// will set the attributes, table, and connections on it so it will work.
$pivots = [];
foreach ($models as $model) {
$model->setRelation($this->accessor, $this->newExistingPivot(
$pivots[] = $pivot = $this->newExistingPivot(
$this->migratePivotAttributes($model)
));
);
$model->setRelation($this->accessor, $pivot);
}

if (count($pivots) > 0) {
$query = $pivots[0]->newQuery();
$query->eagerLoadRelations($pivots);
}
}

Expand Down
28 changes: 28 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,20 @@ public function testBelongsToManyCustomPivot()
$this->assertSame('Jule Doe', $johnWithFriends->friends->find(4)->pivot->friend->name);
}

public function testBelongsToManyCustomPivotUsingWithAttribute()
{
$john = EloquentTestUserWithCustomFriendPivotUsingWithAttribute::create(['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@example.com']);
$jane = EloquentTestUserWithCustomFriendPivotUsingWithAttribute::create(['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@example.com']);

EloquentTestFriendLevel::create(['id' => 1, 'level' => 'acquaintance']);

$john->friends()->attach($jane, ['friend_level_id' => 1]);

$johnWithFriends = EloquentTestUserWithCustomFriendPivotUsingWithAttribute::with('friends')->find(1);

$this->assertTrue($johnWithFriends->friends->first()->pivot->relationLoaded('level'));
}

public function testIsAfterRetrievingTheSameModel()
{
$saved = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
Expand Down Expand Up @@ -1987,6 +2001,15 @@ public function friends()
}
}

class EloquentTestUserWithCustomFriendPivotUsingWithAttribute extends EloquentTestUser
{
public function friends()
{
return $this->belongsToMany(EloquentTestUser::class, 'friends', 'user_id', 'friend_id')
->using(EloquentTestFriendPivotUsingWithAttribute::class)->withPivot('user_id', 'friend_id', 'friend_level_id');
}
}

class EloquentTestUserWithSpaceInColumnName extends EloquentTestUser
{
protected $table = 'users_with_space_in_colum_name';
Expand Down Expand Up @@ -2166,6 +2189,11 @@ public function level()
}
}

class EloquentTestFriendPivotUsingWithAttribute extends EloquentTestFriendPivot
{
protected $with = ['level'];
}

class EloquentTouchingUser extends Eloquent
{
protected $table = 'users';
Expand Down