From 5ae58ae26328fbac576b4a9b276d61cb6790f7f8 Mon Sep 17 00:00:00 2001 From: Lonny Kapelushnik Date: Tue, 27 Jul 2021 15:12:34 -0600 Subject: [PATCH 1/2] Eager load pivot relations --- .../Eloquent/Relations/BelongsToMany.php | 11 ++++++-- .../DatabaseEloquentIntegrationTest.php | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 32bdf8843a0d..05e9837866e6 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -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); } } diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 9624af06add4..670628f3050c 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -1363,6 +1363,20 @@ public function testBelongsToManyCustomPivot() $this->assertSame('friend', $johnWithFriends->friends->find(3)->pivot->level->level); $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() { @@ -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'; @@ -2166,6 +2189,11 @@ public function level() } } +class EloquentTestFriendPivotUsingWithAttribute extends EloquentTestFriendPivot +{ + protected $with = ['level']; +} + class EloquentTouchingUser extends Eloquent { protected $table = 'users'; From dacef6b86bca22739a398dd5b712a08c4ffaa08b Mon Sep 17 00:00:00 2001 From: Lonny Kapelushnik Date: Tue, 27 Jul 2021 15:24:43 -0600 Subject: [PATCH 2/2] Styles fix --- tests/Database/DatabaseEloquentIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 670628f3050c..da9a80fc7bc3 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -1363,7 +1363,7 @@ public function testBelongsToManyCustomPivot() $this->assertSame('friend', $johnWithFriends->friends->find(3)->pivot->level->level); $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']);