title: Flat Laravel collection to tree
body: <p>I have a Laravel collection of pages - each page has a &quot;parent_id&quot; property.  It resembles this.</p>
  <pre><code>&quot;pages&quot;: [
      {
  
          &quot;id&quot;: 1,
          &quot;title&quot;: &quot;Page 1 Level 1&quot;,
          &quot;parent_id&quot;: 0
      },
      {
          &quot;id&quot;: 2,
          &quot;title&quot;: &quot;Page 2 Level 2&quot;,
          &quot;parent_id&quot;: 1
      },
      {
          &quot;id&quot;: 3,
          &quot;title&quot;: &quot;Page 3 Level 3&quot;,
          &quot;parent_id&quot;: 2
      },
      {
          &quot;id&quot;: 4,
          &quot;title&quot;: &quot;Page 4 Level 1&quot;,
          &quot;parent_id&quot;: 0
      },
      {
          &quot;id&quot;: 5,
          &quot;title&quot;: &quot;Page 5 Level 2&quot;,
          &quot;parent_id&quot;: 4
      },
      {
          &quot;id&quot;: 6,
          &quot;title&quot;: &quot;Page 6 Level 3&quot;,
          &quot;parent_id&quot;: 5
      },
      {
          &quot;id&quot;: 7,
          &quot;title&quot;: &quot;Page 7 Level 1&quot;,
          &quot;parent_id&quot;: 0
      },
      {
          &quot;id&quot;: 8,
          &quot;title&quot;: &quot;Page 8 Level 2&quot;,
          &quot;parent_id&quot;: 7
      }
  ]
  </code></pre>
  <p>What I am trying to do is format the output so they are nested with the correct hierarchy. So for example:</p>
  <pre><code>&quot;pages&quot;: [
      {
          &quot;id&quot;: 1,
          &quot;title&quot;: &quot;Page 1 Level 1&quot;,           
          &quot;parent_id&quot;: 0,
          &quot;children&quot;: [
              {
                  &quot;id&quot;: 2,
                  &quot;title&quot;: &quot;Page 2 Level 2&quot;,           
                  &quot;parent_id&quot;: 1,
                  &quot;children&quot;: [
                      {
                          &quot;id&quot;: 3,
                          &quot;title&quot;: &quot;Page 3 Level 3&quot;,           
                          &quot;parent_id&quot;: 2,
                          &quot;children&quot;: []
                      }
                  ]
              },
          ]
      },
      {
          &quot;id&quot;: 4,
          &quot;title&quot;: &quot;Page 4 Level 1&quot;,           
          &quot;parent_id&quot;: 0,
          &quot;children&quot;: [
              {
                  &quot;id&quot;: 5,
                  &quot;title&quot;: &quot;Page 5 Level 2&quot;,           
                  &quot;parent_id&quot;: 4,
                  &quot;children&quot;: [
                      {
                          &quot;id&quot;: 6,
                          &quot;title&quot;: &quot;Page 6 Level 3&quot;,           
                          &quot;parent_id&quot;: 5,
                          &quot;children&quot;: []
                      }
                  ]
              },
          ]
      },
      {
          &quot;id&quot;: 7,
          &quot;title&quot;: &quot;Page 7 Level 1&quot;,           
          &quot;parent_id&quot;: 0,
          &quot;children&quot;: [
              {
                  &quot;id&quot;: 8,
                  &quot;title&quot;: &quot;Page 8 Level 2&quot;,           
                  &quot;parent_id&quot;: 7,
                  &quot;children&quot;: []
              },
          ]
      },
  ]
  </code></pre>
  <p>The hierarchy can be any number of levels deep.  I have a nearly working version as shown below, but it does contain a bug. Whilst the various child objects are nesting with their parent, they also remain at the root level. So it looks like duplicates are actually placed in their nested positions.</p>
  <p>Can anyone help me finish this off?</p>
  <p><strong>PageController</strong></p>
  <pre class="lang-php prettyprint-override"><code>$pages = Page::with('children')-&gt;get();
  </code></pre>
  <p><strong>Page</strong></p>
  <pre class="lang-php prettyprint-override"><code>public function directChildren(): HasMany
  {
      return $this-&gt;hasMany($this, 'parent_id', 'id');
  }
  
  public function children(): HasMany
  {
      return $this-&gt;directChildren()-&gt;with('children'));
  }
  </code></pre>