How to use eager loading for related models in Laravel
<p>I have the following models:</p>

<ul>
<li>Appointment [ belongsTo Timeslot  |  belongsTo User ]</li>
<li>Timeslot [ hasMany Appointment ]</li>
<li>User [ hasOne Appointment  |  HasOne Details ]</li>
<li>Details [ belongsTo User  ]</li>
</ul>

<p>I am trying to eager load the Details data starting from the Appointment model ( Appointment has a user, which has a Details record) using the following query:</p>

<pre><code>$apps = Appointment::with('timeslot')-&gt;with('user')-&gt;with('user.details')-&gt;get();
</code></pre>

<p>This throws the following error in Builder.php</p>

<blockquote>
  <p>Call to a member function addEagerConstraints() on a non-object</p>
</blockquote>

<p>Why am I calling on a non-object here and why is my query not working?</p>

<p>EDIT:</p>

<p>This is the relation on my User model:</p>

<pre><code>public function details() {
    dd($this-&gt;role_id);
    switch($this-&gt;role_id) {
        case 3:
            return $this-&gt;hasOne('App\CandidateDetails', 'user_id');
            break;
        case 2:
            return $this-&gt;hasOne('App\EmployerDetails', 'user_id');
            break;
    }

}
</code></pre>

<p>i'm aware that this would be better implemented using a pivot table, it's been a learning process. the dd() returns a null when called by my query, yet it works fine in other calls. What's going on here? </p>