title: Hierarchical data with different levels of depth - recursive function
body: <p>I have a hierarchical table of vehicles. I want to write recursive function which find all descendants of given id. I want to do it without using path column. For example: find_children(2) sholud return array of ids [6,7,14,10,11,12,13,17,18,19,15,16] </p>
  
  <p><a href="https://i.stack.imgur.com/Y1eHH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y1eHH.jpg" alt="enter image description here"></a></p>
  
  <p>I wrote function</p>
  
  <pre><code>public static function find_children ($id)
  {
      is_array($id)?$ids=$id:$ids[]=$id;
      static $tab=[];
      $results=DB::table('vehicles')-&gt;whereIn('parent_id',$ids)-&gt;pluck('id');
      $how_many=DB::table('vehicles')-&gt;whereIn('parent_id',$ids)-&gt;count();
      if($how_many==0){
          return $tab;
      }
  
      $tab[]=$results;
      return  self::find_children($res);
  }
  </code></pre>
  
  <p>For given example I receive  array of [6,7,14] in first iteration and array of [10,11] in the second one. I think the problem is different levels of depth in hierarchy. I don't know how to come back from level (a3,a4) up to (audi) and then (bmw)
  Please give me some help. </p>