Closed benrowe closed 5 years ago
Since the package just simplifies the model name and uses the Eloquent Relation
class internally, you should be able to use $model->getMorphClass()
to get the registered alias.
In most projects I even use a trait to wrap that method and provide it as an attribute:
<?php
declare(strict_types=1);
namespace App\Models;
trait HasAType
{
/**
* @return string
*/
public function getTypeAttribute() : string
{
return $this->getMorphClass();
}
}
If you want to get it from a class name:
app(\App\Models\Model::class)->getMorphClass();
Can you check if that works?
Cheers
Hey @sebastiaanluca
quick question... any idea of why this output is being generated?
app(\App\Media::class)->getMorphClass();
outputs "medium"
Is it the expected behaviour? I would expect 'media' to be returned...
That would be due to the active naming scheme. See the config file:
/**
* The naming scheme to use when determining the model's morph type
* base value. Defaults to singular table name.
*/
'naming' => NamingSchemes::SINGULAR_TABLE_NAME,
So the mapper takes the table name and singularizes it using Laravel's helper. In effect, media (plural) becomes medium (singular). You can disable this by either defining the morph type for media manually or using another naming scheme.
Sorry @sebastiaanluca actually the behaviour is correct.
We just did not know "medium" was singular of "Media" lol...
Some other colleagues did not know that too so we were a bit confused...
Thanks
Description
It would be nice if I could pass through a either the FQCN or the an object and retrieve the morph as a string.
app(Mapper::class)->getMorphForModel(\App\SomeModel::class)
and/or
app(Mapper::class)->getMorphForModelObject($modelInstance)
This would be handy in cases of handling events, etc where you need to filter the model by a specific relationship type.