I'm building a package which heavily relies on a morph many relationship. As usual with this relationship it is required to define the relationships like so:
public function foos()
{
return $this->morphToMany('App\Models\Foo', 'barable');
}
This obviously works fine and there is no problem here.
The thing is though that there are many of these relationships that need to be defined. And I'd like to just loop through them and building them automagically to make configuring the package easier.
I have tried the following:
public function __get($name)
{
if($name == 'foos') {
return $this->morphToMany('App\Models\Foo', 'barable');
}
}
This does not initiate the query to retrieve the data. It gets called, but it doesn't return data.
The __call function seemed logical to me too, but that just breaks Laravel. As far as I can tell it picks up everything that's being called in the class.
An alternative now is to include a trait and make the programmer fill in these relations in the publishable file, but that just feels wrong.
Any suggestions?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire