title: Recursive function to delete nested categories in Laravel
body: <p>I have this table</p>
  
  <pre>
  id - parent_id - name 
  1 - 0 - T-shirts 
  2 - 1 - Red T-shirts 
  3 - 1 - Black T-shirts 
  4 - 0 - Sweaters 
  5 - 4 - Red Sweaters 
  6 - 4 - Blue
  </pre>
  
  <p>There can be six nested categories</p>
  
  <p>recursive function that delete all nested categorie by parent id when i choose to delete an id</p>
  
  <p>example when i delete tshirt , both red t-shirt and black-tshirt should be deleted to and there nested categorie
  Mohamed Amine
  i write those function but there blocking page
  Mohamed Amine
  to take all the ids i want to delete and store in an array to destroy all togather in once </p>
  
  <p>voila functions</p>
  
  <hr>
  
  <pre><code>protected $theArray = array();
  
  public function recursiveDelete($v)
  {
    $toRecurses = Car::where('parent_id', $v )-&gt;get();
    foreach( $toRecurses as $toRecurse ){
      array_push( $this-&gt;theArray, $toRecurse-&gt;id );
    }
    foreach( $toRecurses as $toRecurse ){
      if( Car::where('parent_id' , $toRecurse-&gt;id)-&gt;get()-&gt;first() ){
        return $this-&gt;recursiveDelete( $toRecurse );
        //dd( Car::where('parent_id' , $toRecurse-&gt;id)-&gt;get()-&gt;first() );
      }
    }
  }
  
  public function testForDeletingCar(Car $carD, $id)
  {
    $c = $carD::where('id' , $id)-&gt;get()-&gt;first();
    $this-&gt;recursiveDelete( $c-&gt;id );
    return $this-&gt;theArray;
  }
  </code></pre>