title: Laravel delete by BelongsToMany relationship
body: <p>I have a <code>Task</code> class. And each record of this table can have one or many childs and can be one or many parents of another task. </p>
  
  <p>Table <strong>tasks</strong></p>
  
  <pre><code>id | name 
  </code></pre>
  
  <p>Table <strong>tasks_links</strong></p>
  
  <pre><code>parent_id | child_id
  </code></pre>
  
  <p>Task model</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  class Task extends Model
  {    
      public function childs()
      {
          return $this-&gt;belongsToMany(Task::class, 'tasks_links',  'parent_id','child_id');
      }
  
      public function parents()
      {
          return $this-&gt;belongsToMany(Task::class, 'tasks_links' , 'child_id' , 'parent_id');
      }
  }
  </code></pre>
  
  <p>And in my controller and views
  I can use the relationship like</p>
  
  <pre><code>Task::find($id)-&gt;parents(); //Return an array
  </code></pre>
  
  <p>When I delete a task, I would also to delete the links with other related tasks</p>
  
  <p>So the following is working : </p>
  
  <pre><code>\DB::table('tasks_links')-&gt;where('parent_id' , $task-&gt;id)-&gt;delete();
  \DB::table('tasks_links')-&gt;where('child_id' , $task-&gt;id)-&gt;delete();
  </code></pre>
  
  <p>But I the following is not working</p>
  
  <pre><code>foreach ($task-&gt;parents() as $parent) {
          $parent-&gt;delete();
      }
  </code></pre>
  
  <p>Is there any way to delete the <code>links</code> by using the <code>parents</code> and <code>childs</code> functions instead of searching directly in the database ?</p>