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 fail to morph with custom casting to objects #35420

Merged
merged 1 commit into from Nov 30, 2020
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->{$type})
return is_null($class = $this->getAttributeFromArray($type))
? $this->morphEagerTo($name, $type, $id, $ownerKey)
: $this->morphInstanceTo($class, $name, $type, $id, $ownerKey);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentModelTest.php
Expand Up @@ -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());
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentMorphToTest.php
Expand Up @@ -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');

Expand Down