lundi 24 octobre 2016

laravel relation - variable local key and eager loading

My situation - logical data model

I have situation like this:

  • Entity1 can have many other entities1
  • If entity1 is not root then it belongs to root entity1
  • If entity1 is root then it have one descriptor

Now I want to access to descriptor model from entity model. If entity is not root then it should have its root entity descriptor.

I have done something like this:

class Entity1 extends Model {
   public function descriptor(){
     if ($this->root_id)
         $key = 'root_id';
     else
         $key = 'id';
     return $this->hasOne(Descriptor::class,'id',$key)
   }
}

But now I have a problem with eager loading descriptor. How to solve problem with eager loading?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire