title: Laravel - Sync only a subset of the pivot table
body: <p>My pivot table contains a total of 3 columns:</p>
  
  <ul>
  <li>user_id</li>
  <li>role_id</li>
  <li>group</li>
  </ul>
  
  <p>Group is just an integer. I would like to be able to sync the users and their roles but only the ones that belong to a specific group.</p>
  
  <p>If I run a simple sync([1,2,3]) it will remove everything from the pivot table, ignoring the group altogether.</p>
  
  <p>I have a few solutions in mind:</p>
  
  <p>Option a:</p>
  
  <ol>
  <li>Create a new model for UserRoles.</li>
  <li><code>UserRoles::where('group', '=', '1');</code></li>
  <li><code>User::roles()-&gt;detach(list_of_ids_from_previous_query);</code></li>
  <li><code>User::roles()-&gt;attach(list_of_desired_ids_for_group_1);</code></li>
  </ol>
  
  <p>Option b:</p>
  
  <ol>
  <li><code>User::roles()-&gt;all();</code></li>
  <li>Fancy merge <code>$list_of_desired_ids_for_group_1</code> with  <code>$list_of_ids_from_previous_query</code></li>
  <li><code>User::roles()-&gt;sync(list_of_merged_ids);</code></li>
  </ol>
  
  <p>Is there another way to do this with Eloquent? I reckon option (a) is easier to implement as I don't have to merge 2 multidimensional arrays of IDs and groups. But also, option (a) might be more database intensive as it needs to run a DELETE and INSERT on all group rows.</p>