title: laravel relation - variable local key and eager loading
body: <p><a href="https://i.stack.imgur.com/5uFh2.jpg" rel="nofollow">My situation - logical data model</a></p>
  
  <p>I have situation like this:</p>
  
  <ul>
  <li>Entity1 can have many other entities1</li>
  <li>If entity1 is not root then it belongs to root entity1</li>
  <li>If entity1 is root then it have one descriptor</li>
  </ul>
  
  <p>Now I want to access to descriptor model from entity model. If entity is not root then it should have its root entity descriptor.</p>
  
  <p>I have done something like this:</p>
  
  <pre><code>class Entity1 extends Model {
     public function descriptor(){
       if ($this-&gt;root_id)
           $key = 'root_id';
       else
           $key = 'id';
       return $this-&gt;hasOne(Descriptor::class,'id',$key)
     }
  }
  </code></pre>
  
  <p>But now I have a problem with eager loading descriptor.
  How to solve problem with eager loading?</p>