title: How to loop through this trees? laravel php nested sets recursively (SAME MODEL)
body: <p>How can I loop through this tree node, in laravel??
  It took me couple of days to get it to work but it doesn't!!
  Here is the table structure:</p>
  
  <pre><code>id        parent_id        text;
  1         0                parent;
  2         1                child;
  3         2                descendant;
  4         0                parent;
  </code></pre>
  
  <p>`
  And so on.... </p>
  
  <p>Here is the model relationship</p>
  
  <pre><code>public function parent()
  {
      return $this-&gt;belongsTo('App\ContextMenuItem','parent_id')-&gt;where('parent_id',0);
  }
  
  public function children()
  {
      return $this-&gt;hasMany('App\ContextMenuItem','parent_id');
  }
  </code></pre>
  
  <p>Here is the function that I don't know how to use it to get the items recursively.</p>
  
  <pre><code>public static function getTree(string $id, array $tree = []): array
  {
      $lowestLevel = ContextMenuItem::where('id', $id)-&gt;first();
  
      if (!$lowestLevel) {
          return $tree;
      }
  
      $tree[] = $lowestLevel-&gt;toArray();
  
      if ($lowestLevel-&gt;parent_id !== 0) {
          $tree = ContextMenuItem::getTree($lowestLevel-&gt;parent_id, $tree);
      }
  
      return $tree;
  
  }
  </code></pre>