In my use case, I have a Language model, whose display name can be translated into any other language:
/**
* @property int $id
* @property string $code
* @property-read string $self_display_name
*/
class Language extends RestfulModel
{
protected $appends = [
'self_display_name',
];
/**
* Returns the own displayable name of the language.
*
* @return string
*/
public function getSelfDisplayNameAttribute(): string
{
return $this->getDisplayName($this->code);
}
/**
* Returns a displayable name for the language, translated into another given language.
*
* @param string $languageCode
* @return string
*/
public function getDisplayName(string $languageCode): string
{
return Str::ucfirst(Str::lower(\Locale::getDisplayLanguage($this->code, $languageCode)));
}
}
and a transformer that allows the client to specify the languages into which the display names should be translated:
In my use case, I have a
Language
model, whose display name can be translated into any other language:and a transformer that allows the client to specify the languages into which the display names should be translated:
But because
displayNames
is not a relationship, queries fail whenever the corresponding include is requested.