Skip to content

Commit

Permalink
fix: resolve morphed model when present in morph map
Browse files Browse the repository at this point in the history
  • Loading branch information
IsraelOrtuno committed Jun 16, 2023
1 parent 844f73d commit ec80661
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/CastsModel.php
Expand Up @@ -44,6 +44,12 @@ public function getMorphClass()
return parent::getMorphClass();
}

$morphClass = parent::getMorphClass();

if ($morphClass != static::class) {
return $morphClass;
}

return $this->getCastableModel()->getMorphClass();
}

Expand Down
13 changes: 12 additions & 1 deletion tests/CastsModelTest.php
Expand Up @@ -5,6 +5,7 @@
use Devio\ModelCast\Tests\Support\TabletDeviceModelTest;
use Devio\ModelCast\Tests\Support\PhoneDeviceModelTest;
use Devio\ModelCast\Tests\Support\DeviceModelTest;
use Illuminate\Database\Eloquent\Relations\Relation;

it('casts a model when retrieving a single record', function () {
DeviceModelTest::create(['name' => 'phone']);
Expand Down Expand Up @@ -44,8 +45,18 @@
expect($model->getTable())->toBe('device_models');
});

it('provides castable model morph class', function() {
it('provides castable model morph class by default', function () {
$model = new PhoneDeviceModelTest();

expect($model->getMorphClass())->toBe($model->getCastableModelClass());
});

it('provides casted model morph class when present in morph map', function () {
Relation::morphMap([
'tablet' => TabletDeviceModelTest::class
]);

$model = new TabletDeviceModelTest();

expect($model->getMorphClass())->toBe('tablet');
});

0 comments on commit ec80661

Please sign in to comment.