From 8f157ccdb664e97ddb64ef0192635fe2efb64fd7 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 27 Sep 2024 13:37:01 +0330 Subject: [PATCH 1/3] Use owner key if it was set; --- src/Relations/MorphTo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Relations/MorphTo.php b/src/Relations/MorphTo.php index 692991372..4874b23bb 100644 --- a/src/Relations/MorphTo.php +++ b/src/Relations/MorphTo.php @@ -29,7 +29,7 @@ protected function getResultsByType($type) { $instance = $this->createModelByType($type); - $key = $instance->getKeyName(); + $key = $this->ownerKey ?? $instance->getKeyName(); $query = $instance->newQuery(); From 327a51420300ae286efb51cf4386d356100a2d7b Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 27 Sep 2024 13:48:24 +0330 Subject: [PATCH 2/3] Add tests; --- tests/RelationsTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 902f0499c..7e8a2ac67 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -634,11 +634,13 @@ public function testMorph(): void $photo = Photo::first(); $this->assertEquals($photo->hasImage->name, $user->name); + // eager load $user = User::with('photos')->find($user->id); $relations = $user->getRelations(); $this->assertArrayHasKey('photos', $relations); $this->assertEquals(1, $relations['photos']->count()); + // inverse eager load $photos = Photo::with('hasImage')->get(); $relations = $photos[0]->getRelations(); $this->assertArrayHasKey('hasImage', $relations); @@ -648,7 +650,7 @@ public function testMorph(): void $this->assertArrayHasKey('hasImage', $relations); $this->assertInstanceOf(Client::class, $photos[1]->hasImage); - // inverse + // inverse relationship $photo = Photo::query()->create(['url' => 'https://graph.facebook.com/hans.thomas/picture']); $client = Client::create(['name' => 'Hans Thomas']); $photo->hasImage()->associate($client)->save(); @@ -666,6 +668,14 @@ public function testMorph(): void $this->assertInstanceOf(Client::class, $photo->hasImageWithCustomOwnerKey); $this->assertEquals($client->cclient_id, $photo->has_image_with_custom_owner_key_id); $this->assertEquals($client->id, $photo->hasImageWithCustomOwnerKey->id); + + // inverse eager load with custom ownerKey + $photos = Photo::with('hasImageWithCustomOwnerKey')->get(); + $check = $photos->last(); + $relations = $check->getRelations(); + $this->assertArrayHasKey('hasImageWithCustomOwnerKey', $relations); + $this->assertInstanceOf(Client::class, $check->hasImageWithCustomOwnerKey); + } public function testMorphToMany(): void From d5bf3a202bc618fcfbf284d74ffef7c214021462 Mon Sep 17 00:00:00 2001 From: hans-thomas Date: Fri, 27 Sep 2024 10:23:05 +0000 Subject: [PATCH 3/3] apply phpcbf formatting --- tests/RelationsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 7e8a2ac67..a58fef02f 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -675,7 +675,6 @@ public function testMorph(): void $relations = $check->getRelations(); $this->assertArrayHasKey('hasImageWithCustomOwnerKey', $relations); $this->assertInstanceOf(Client::class, $check->hasImageWithCustomOwnerKey); - } public function testMorphToMany(): void