title: Many-to-Many Eloquent relationship update with Laravel Form Model Binding & Checkboxes
body: <p>I have 3 tables:</p>
  
  <p><strong>doors</strong></p>
  
  <ul>
  <li>id</li>
  <li>name</li>
  <li>image</li>
  </ul>
  
  <p><strong>colors</strong></p>
  
  <ul>
  <li>id</li>
  <li>name</li>
  <li>image</li>
  </ul>
  
  <p><strong>door_colors</strong></p>
  
  <ul>
  <li>id</li>
  <li>door_id</li>
  <li>color_id</li>
  </ul>
  
  <p>and 2 models with a many-to-many relationship (each door comes in a variety colors, and many colors overlap door-to-door):</p>
  
  <p><strong>Door Model</strong></p>
  
  <pre><code>class Door extends Eloquent {
      public function colors()
      {
          return $this-&gt;belongsToMany('Color', 'door_colors');
      }
  }
  </code></pre>
  
  <p><strong>Color Model</strong></p>
  
  <pre><code>class Color extends Eloquent {
      public function doors()
      {
          return $this-&gt;belongsToMany('Door', 'door_colors');
      }
  }
  </code></pre>
  
  <p>I want to create a form where I can edit the door, and update the available colors via checkboxes.
  This is my <strong>Admin Doors Controller</strong></p>
  
  <pre><code>class AdminDoorsController extends AdminController {
      public function edit($id)
      {
          $data['door'] = Door::find($id);
          $data['colors'] = Color::all();
          return View::make('admin/doors/form', $data);
      }
  }
  </code></pre>
  
  <p>and the <strong>Admin Doors Form View</strong></p>
  
  <pre><code>{{ Form::model($door) }}
  Colors:
  @foreach ($colors as $color)
  {{ Form::checkbox('colors[]', $color-&gt;id) }} {{ $color-&gt;name }}
  @endforeach
  {{ Form::close() }}
  </code></pre>
  
  <p><strong>Question 1:</strong> How do I make it so that as the checkboxes are outputted, the ones with an existing relationship with the current door are checked and the ones without are unchecked.</p>
  
  <p><strong>Question 2:</strong> Once I check the boxes and hit submit, how would I update the relationships? <code>$door-&gt;colors()-&gt;detach();</code> to clear all existing ones for this door, then <code>$door-&gt;colors()-&gt;attach($color_id_array);</code> to create new ones based on an array of color ids?</p>
  
  <p>Any input is appreciated!</p>