Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
alcaeus marked this conversation as resolved.
{
// 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
Expand All @@ -53,7 +53,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
}

/**
* Define an embedded one-to-many relationship.
* Define an embedded one-to-one relationship.
*
* @param class-string $related
* @param string|null $localKey
Expand All @@ -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)
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

embedsOne()'s docblock says "Define an embedded one-to-many relationship", but this method returns EmbedsOne and represents an embedded one-to-one relationship. Please update the docblock description to avoid misleading API docs now that the method is public.

Copilot uses AI. Check for mistakes.
{
// 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
Expand Down
24 changes: 24 additions & 0 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading