title: Laravel: nested "with()" function
body: <p>I want to retrieve data from three tables: courses, competencies and competency_standards. The following query almost works, but it does not bring back the associated <code>competency_standards</code> table data.</p>
  
  <pre><code>    $coursesAndComps = Course::with(
          array('competencies' =&gt; function($query)
          {
              Competency::with('competency_standards');
          })
      )-&gt;get()-&gt;toArray();
  </code></pre>
  
  <p>Where the <code>competencies</code> table is linked to the <code>courses</code> table (competencies.course_id = course.id) and the <code>competency_standards</code> table links to the <code>competencies</code> table (competency_standards.competencey_id = competency.id).</p>
  
  <p>The array returned looks like this:</p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [id] =&gt; 1
          [name] =&gt; the first course
          [competencies] =&gt; Array
          (
              [0] =&gt; Array
              (
                  [id] =&gt; 9
                  [course_id] =&gt; 1
                  [name] =&gt; first course comp 1 
              )
  
              [1] =&gt; Array
              (
                  [id] =&gt; 10
                  [course_id] =&gt; 1
                  [name] =&gt; first course comp 2
              )
  
          )
      )
  
  )
  </code></pre>
  
  <p>but within the <code>competencies</code> array, I was hoping to find another array called <code>competency_standards</code> like this:</p>
  
  <p>...</p>
  
  <pre><code>[0] =&gt; Array
      (
          [id] =&gt; 9
          [course_id] =&gt; 1
          [name] =&gt; first course comp 1 
          [competency_standards] =&gt; Array
              (
                  [0] =&gt; Array
                  (
                      [id] =&gt; 22
                      [competency_id] =&gt; 9
                      [name] =&gt; standard foo 
                  )
                  [1] =&gt; Array
                  (
                      [id] =&gt; 23
                      [competency_id] =&gt; 9
                      [name] =&gt; standard bar 
                  )
              )
      )
  </code></pre>
  
  <p>...</p>
  
  <p>Is this possible? Am I going about this the wrong way?</p>