I have a trait called Multilingual which uses the model its lang and translation_of attributes (also see https://stackoverflow.com/a/7299777/1453912) to provide multiple translations of an entity.
Now I want to hide the translation_of field from the model when $model->toArray() is called, which is - most easily - done through adding it to the $hidden attribute. Like this:
class Model {
use Multilingual;
protected $hidden = ['translation_of'];
}
But to keep the original model clean, I want to add the hidden field via the used trait.
I have tried:
Adding
protected $hidden = ['translation_of'];to the trait, which is not allowed:Undefined: trait declaration of property 'hidden' is incompatible with previous declarationAnd also not very extensible (It will be overridden by the
$hiddenproperty of the class, I think..)Adding a boot method to the trait:
static function bootMultilingual() { static::$hidden[] = 'translation_of'; }Which (as I suspected) is also not allowed, because of the scope.
Any ideas for a clean way to do this?
Please help!
NOTE: To keep it dynamic, I figured it can be done in two ways:
- Internally:
$this->hidden[] = 'translation_of'; - Externally:
$model->setHidden(array_merge($model->getHidden(), ['translation_of']));