I want to have a dynamic relationship based on a class name, which is stored as a property on a far related model.
There are lots of different class names so I don't want to make lots of relationships. It's a bit complicated but it has to be this way.
function grandchildren() {
$className = $this->parent->grandparent->class_name;
return $this->hasMany($class)->orderBy('value');
}
The problem is this trying to do it this way won't work mid query because it doesn't always have the 'child' model information yet (as ->get() hasn't been run). Another issue with this, it's it's running the same $this->parent->grandparent query every time - not ideal.
Before I start the relationship query, I can run this to get the class name and table name respectively:
$className = $parentModel->grandparent->class_name;
$tableName = ( new $className )->getTable();
But how to feed this into the relationship I'm not yet sure:
$parent->child()->with('grandchildren')->get();
Without creating a lot of relationships for each possible class, how do I feed the $className in to create a dynamic hasMany relationship, or can I do this by adding a new query scope and feeding in the $tableName or $className that way?
I hope this makes sense - thanks in advance for any assistance.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2laoAgG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire