Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fix morphTo attempting to map an empty string morph type to an instance #35824

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -256,7 +256,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. In this case we'll just pass in a dummy query where we
// need to remove any eager loads that may already be defined on a model.
return is_null($class = $this->getAttributeFromArray($type))
return is_null($class = $this->getAttributeFromArray($type)) || $class === ''
? $this->morphEagerTo($name, $type, $id, $ownerKey)
: $this->morphInstanceTo($class, $name, $type, $id, $ownerKey);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentMorphToTest.php
Expand Up @@ -100,6 +100,16 @@ public function testMorphToWithZeroMorphType()
$parent->relation();
}

public function testMorphToWithEmptyStringMorphType()
{
$parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttributeFromArray', 'morphEagerTo', 'morphInstanceTo'])->getMock();
$parent->method('getAttributeFromArray')->with('relation_type')->willReturn('');
$parent->expects($this->once())->method('morphEagerTo');
$parent->expects($this->never())->method('morphInstanceTo');

$parent->relation();
}

public function testMorphToWithSpecifiedClassDefault()
{
$parent = new EloquentMorphToModelStub;
Expand Down