From e2a92045c6c1bf1dd263989ba7e2af6e5f31d17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 30 Aug 2023 17:03:48 +0200 Subject: [PATCH 1/2] Make EmbedsRelations::embedsMany and EmbedsRelations::embedsOne public --- src/Eloquent/EmbedsRelations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Eloquent/EmbedsRelations.php b/src/Eloquent/EmbedsRelations.php index d5984b08e..5b3d05fe3 100644 --- a/src/Eloquent/EmbedsRelations.php +++ b/src/Eloquent/EmbedsRelations.php @@ -28,7 +28,7 @@ trait EmbedsRelations * * @return EmbedsMany */ - protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null) + public function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null) { // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most @@ -62,7 +62,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r * * @return EmbedsOne */ - protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null) + public function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null) { // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most From 6a257ca898d495bc06838751658a0b5ea6b890c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 20 Apr 2026 17:25:07 +0200 Subject: [PATCH 2/2] Fix docblock for embedsOne and add tests for public visibility - Fix embedsOne docblock: "one-to-many" -> "one-to-one" - Add tests for direct call and resolveRelationUsing use case --- src/Eloquent/EmbedsRelations.php | 2 +- tests/EmbeddedRelationsTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Eloquent/EmbedsRelations.php b/src/Eloquent/EmbedsRelations.php index 5b3d05fe3..da02cf7ae 100644 --- a/src/Eloquent/EmbedsRelations.php +++ b/src/Eloquent/EmbedsRelations.php @@ -53,7 +53,7 @@ public function embedsMany($related, $localKey = null, $foreignKey = null, $rela } /** - * Define an embedded one-to-many relationship. + * Define an embedded one-to-one relationship. * * @param class-string $related * @param string|null $localKey diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index 1c68e2d34..ac84851d9 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -972,4 +972,28 @@ public function testUnsetPropertyOnEmbed() $this->assertNull($user->addresses->get(0)->city); $this->assertSame('Kyoto', $user->addresses->get(1)->city); } + + public function testResolveRelationUsingEmbedsMany() + { + User::resolveRelationUsing('dynamicAddresses', fn (User $model) => $model->embedsMany(Address::class, 'addresses', 'user_id', 'dynamicAddresses')); + + $user = User::create(['name' => 'John Doe']); + $user->dynamicAddresses()->save(new Address(['city' => 'London'])); + + $user = User::find($user->id); + $this->assertCount(1, $user->dynamicAddresses); + $this->assertEquals('London', $user->dynamicAddresses->first()->city); + } + + public function testResolveRelationUsingEmbedsOne() + { + User::resolveRelationUsing('dynamicFather', fn (User $model) => $model->embedsOne(User::class, 'father', 'user_id', 'dynamicFather')); + + $user = User::create(['name' => 'John Doe']); + $user->dynamicFather()->save(new User(['name' => 'Mark Doe'])); + + $user = User::find($user->id); + $this->assertNotNull($user->dynamicFather); + $this->assertEquals('Mark Doe', $user->dynamicFather->name); + } }