I am working with Laravel Eloquent models and have gotten stuck on an inheritence issue.
I have got BaseModel
class, which has protected static $dynamicRelations = [];
parameter, and some methods to work with that.
And then I have multiple other classes, extending BaseModel
, for this example let's say PageModel
and EventModel
.
If I create a dynamic relation on the page model, as such PageModel::setDynamicRelation
, it puts it in the static variable:
public static function setDynamicRelation($key, $callback)
{
static::$dynamicRelations[$key] = $callback;
}
This way, I can add a dynamic relationship to the model. So if I do PageModel::setDynamicRelation('banners', ...)
, then on an instance of PageModel
I can call PageModel->banners
to retrieve the relationship values.
The issue is, that the relations are kept in the BaseModel
static parameter, and are inherited by other models. So if I set the relationsip on PageModel
, and then instantiate an EventModel
, it also gets those same relationships, which is not correct.
How can I make it so that the relationships are stored in the child class and are not inherited by other classes? I.e. need to store a copy of $dynamicRelations
on the class that the methods are called upon, so all the children don't share same relations?
Thanks!