2 years ago

Getting Model's morph class without an instance

Posted 2 years ago by 36864

When using a custom morphMap, you can get the morph class for a model using the getMorphClass() instance method. The inverse operation can be found in a static method in the Model class, getActualClassNameForMorph()

Is there a static way to get the result of getMorphClass()? I can get it by creating an instance of the model, but that's not very nice. Using the morphMap array directly also doesn't feel right.

Ideally, I'd like to do something like this:

function morph_name($morphableType){
 return $$morphableType::getMorphClass();

which would work as this currently does:

function morph_name($morphableType){
 return (new $morphableType)->getMorphClass();

I've looked through the API docs and can't find any method that does this.

Please sign in or create an account to participate in this conversation.