From 3bd160559babcf4fb8c8dd83815992e8e12ca2de Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 25 Nov 2020 12:05:57 -0500 Subject: [PATCH 1/2] fix: use strict null check on morphTo type --- src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 049f3b294365..b8e8bcf01adb 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -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 empty($class = $this->{$type}) + return is_null($class = $this->{$type}) ? $this->morphEagerTo($name, $type, $id, $ownerKey) : $this->morphInstanceTo($class, $name, $type, $id, $ownerKey); } From 4a055e72cba9f86cf5da0daf36779f63d3589a5c Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 25 Nov 2020 12:06:23 -0500 Subject: [PATCH 2/2] test: assert 0 relation_type is handled correctly --- tests/Database/DatabaseEloquentMorphToTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 67c584e0fe4e..3f466054e4b5 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -90,6 +90,16 @@ public function testMorphToWithArrayDefault() $this->assertSame('taylor', $result->username); } + public function testMorphToWithZeroMorphType() + { + $parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttribute', 'morphEagerTo', 'morphInstanceTo'])->getMock(); + $parent->method('getAttribute')->with('relation_type')->willReturn(0); + $parent->expects($this->once())->method('morphInstanceTo'); + $parent->expects($this->never())->method('morphEagerTo'); + + $parent->relation(); + } + public function testMorphToWithSpecifiedClassDefault() { $parent = new EloquentMorphToModelStub;