Laravel Model nested query
<p>I was trying to write a single query to get full user diet from database. The problem is that I am not able to get data from the <strong>diet_food</strong> table. Here are my tables:</p>

<ul>
<li><strong>diet_settings</strong> (<em>id, total_days, etc</em>). The basic information about the users diet.</li>
<li><strong>eating_types</strong> (<em>id, diet_id, eating_time, etc</em>). Information about breakfast, lunch etc.</li>
<li><strong>diet_food</strong> (<em>id, eating_type_id, product, etc</em>). Food related to each eating type.</li>
</ul>

<p>This is the query that I use in my controller class:</p>

<pre><code>$diet = Diet::with("eatingType.dietFood")-&gt;get()-&gt;find($id)-&gt;toArray();
</code></pre>

<p>So when I use <strong>var_dump()</strong> this is what it looks like:</p>

<pre><code>array (size=7)
'id' =&gt; int 2
'user_id' =&gt; int 1
'total_days' =&gt; int 1
'total_eating' =&gt; int 6
'notes' =&gt; string '' (length=0)
'created' =&gt; string '2016-09-01' (length=10)
'eating_type' =&gt; 
    array (size=3)
      0 =&gt; 
         array (size=6)
           'id' =&gt; int 7
           'diet_id' =&gt; int 2
           'eating_time' =&gt; string '8:00' (length=4)
           'eating_type' =&gt; string 'Breakfast' (length=10)
           'recommended_rate' =&gt; int 0
           'diet_food' =&gt; 
               array (size=0)
                   ...
      1 =&gt; 
        array (size=6)
          'id' =&gt; int 8
          'diet_id' =&gt; int 2
          'eating_time' =&gt; string '12:00' (length=5)
          'eating_type' =&gt; string 'Lunch' (length=14)
          'recommended_rate' =&gt; int 0
          'diet_food' =&gt; 
              array (size=0)
                 ... 
</code></pre>

<p>In the array the <strong>diet_food</strong> is empty even though I have data stored in the datababase.</p>

<h2>Diet Model</h2>

<pre><code>class Diet extends Model
{
    public $table = "diet_settings";
    public $timestamps = false;

    public function eatingType(){
        return $this-&gt;hasMany(EatingType::class);
    }
}
</code></pre>

<h2>EatingType Model</h2>

<pre><code>class EatingType extends Model
{
    public $timestamps = false;
    public $table = "eating_types";

    public function dietFood(){
        return $this-&gt;hasMany(DietFood::class, 'id','eating_type_id');
    }
    public function diet(){
        return $this-&gt;belongsTo(Diet::class);
    }
}
</code></pre>

<h2>DietFood Model</h2>

<pre><code>class DietFood extends Model
{
    public $timestamps = false;
    public $table = "diet_food";

    public function eatingType(){
        return $this-&gt;belongsTo(EatingType::class);
    }
}
</code></pre>