title: Laravel save / update many to many relationship
body: <p>Can anyone help me on how to save many to many relationship? I have tasks, user can have many tasks and task can have many users (many to many), What I want to achieve is that in <em>update form</em> admin can assign multiple users to specific task. This is done through html multiple select input </p>
  
  <pre><code>name="taskParticipants[]"
  </code></pre>
  
  <p>The catch here is that through the same form (input) you can add/remove users, that's why I have to use sync().
  Maybe I should start from the beginning but don't know where to start... </p>
  
  <p><strong>This is my User model:</strong></p>
  
  <pre><code>public function tasks()
  {
      return $this-&gt;belongsToMany('Task','user_tasks');
  }
  </code></pre>
  
  <p><strong>Task model</strong></p>
  
  <pre><code>public function taskParticipants()
  {
      return $this-&gt;belongsToMany('User','user_tasks');
  }
  </code></pre>
  
  <p><strong>TaskController</strong></p>
  
  <pre><code>public function update($task_id)
  {
      if (Input::has('taskParticipants'))
      {
          foreach(Input::get('taskParticipants') as $worker)
          {
              $task2 = $task-&gt;taskParticipants-&gt;toArray();
              $task2 = array_add($task2,$task_id,$worker);
              $task-&gt;taskParticipants()-&gt;sync(array($task2));
          }
      }
  }
  </code></pre>
  
  <p><strong>This is structure of tables</strong>
      tasks
      id|title|deadline</p>
  
  <pre><code>user_tasks
  id|task_id|user_id
  </code></pre>