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
2 changes: 1 addition & 1 deletion src/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getResultsByType($type)
{
$instance = $this->createModelByType($type);

$key = $instance->getKeyName();
$key = $this->ownerKey ?? $instance->getKeyName();

$query = $instance->newQuery();

Expand Down
11 changes: 10 additions & 1 deletion tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -666,6 +668,13 @@ 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
Expand Down
Loading