diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index b8e8bcf01adb..40a7d890c80c 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 is_null($class = $this->{$type}) + return is_null($class = $this->getAttributeFromArray($type)) ? $this->morphEagerTo($name, $type, $id, $ownerKey) : $this->morphInstanceTo($class, $name, $type, $id, $ownerKey); } diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 114c4ae6f5c2..4551d42cfb51 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1275,6 +1275,7 @@ public function testMorphToCreatesProperRelation() $this->addMockConnection($model); // $this->morphTo(); + $model->setAttribute('morph_to_stub_type', EloquentModelSaveStub::class); $relation = $model->morphToStub(); $this->assertSame('morph_to_stub_id', $relation->getForeignKeyName()); $this->assertSame('morph_to_stub_type', $relation->getMorphType()); @@ -2229,7 +2230,6 @@ class EloquentModelStub extends Model public $scopesCalled = []; protected $table = 'stub'; protected $guarded = []; - protected $morph_to_stub_type = EloquentModelSaveStub::class; protected $casts = ['castedFloat' => 'float']; public function getListItemsAttribute($value) diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 3f466054e4b5..172a2aa84d1d 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -92,8 +92,8 @@ public function testMorphToWithArrayDefault() public function testMorphToWithZeroMorphType() { - $parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttribute', 'morphEagerTo', 'morphInstanceTo'])->getMock(); - $parent->method('getAttribute')->with('relation_type')->willReturn(0); + $parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttributeFromArray', 'morphEagerTo', 'morphInstanceTo'])->getMock(); + $parent->method('getAttributeFromArray')->with('relation_type')->willReturn(0); $parent->expects($this->once())->method('morphInstanceTo'); $parent->expects($this->never())->method('morphEagerTo');