Skip to content

Commit

Permalink
fix(custom cast): should get model attributes without casting prior m…
Browse files Browse the repository at this point in the history
…orph (#35420)

Co-authored-by: Martin Hsia <martin.hsia@asiayo.com>
  • Loading branch information
marandy0905 and Martin Hsia committed Nov 30, 2020
1 parent 2b4b18c commit baa62bb
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
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

0 comments on commit baa62bb

Please sign in to comment.