title: How to save entries in many to many polymorphic relationship in Laravel?
body: <p>I have an Org model and a Tag model. I want to associate tags with organizations. My database tables and Eloquent models are set up like so ...</p>
  
  <pre><code>org
      id - integer
      name - string
      ...
  
  tags
      id - integer
      name - string
  
  taggables
      id - integer
      taggable_id - integer
      taggable_type - string
  
  // app/models/Org.php
  class Org extends Eloquent
  {
      protected $table = "org";
  
      ...
  
      public function tags()
      {
          return $this-&gt;morphToMany('Tag', 'taggable');
      }
  }
  
  // app/models/Tag.php
  class Tag extends Eloquent
  {
      protected $table = "tags";
      public $timestamps = false;
  
      public function org() 
      {
          return $this-&gt;morphedByMany('Org', 'taggable');
      }
  }
  </code></pre>
  
  <p>In my view, I have a form with a multiple select box where the user can select the tags he/she wants to be associated with the organization ...</p>
  
  <pre><code>...
  {{ Form::select('tags[]', $tag_options, null, array(
          'multiple',
          'data-placeholder' =&gt; 'Select some tags'))
  }}
  ...
  </code></pre>
  
  <p>... And $tag_options comes from my routes.php file ...</p>
  
  <pre><code>View::composer('*', function($view)
  {
      $tags = Tag::all();
  
      if(count($tags) &gt; 0)
      {
          $tag_options = array_combine($tags-&gt;lists('id'),
                                      $tags-&gt;lists('name'));
      }
      else
      {
          $tag_options = array(null, 'Unspecified');
      }
  
      $view-&gt;with('tag_options', $tag_options);
  });
  </code></pre>
  
  <p>When the form in my view is submitted, the following route will catch it to update the org model ...</p>
  
  <pre><code>Route::put('org/{org}', function(Org $org){
      $org-&gt;description = Input::get('description');
      $org-&gt;website = Input::get('website');
      $org-&gt;tags = Input::get('tags');
      $org-&gt;save();
  
      return Redirect::to('org/'.$org-&gt;id)
          -&gt;with('message', 'Seccessfully updated page!');
  });
  </code></pre>
  
  <p>Now, Input::get('tags') is just an array of the tag IDs, of the form</p>
  
  <pre><code>["1","6","8"]
  </code></pre>
  
  <p>How can I use this to associate the tags with the organization?</p>
  
  <p>I also have comments set up for organizations using a polymorphic relationship where I just do this ...</p>
  
  <pre><code>Route::put('org/post/{org}', function(Org $org){
      $comment = new Comment;
      $comment-&gt;user_id = Auth::user()-&gt;id;
      $comment-&gt;body = Input::get('body');
      $comment-&gt;commentable_id = $org-&gt;id;
      $comment-&gt;commentable_type = 'Org';
      $comment-&gt;save();
  
      return Redirect::to('org/'.$org-&gt;id)
          -&gt;with('message', 'Seccessfully posted comment!');
  });
  </code></pre>
  
  <p>However, it's not as simple with a many-to-many polymorphic relationship when I want to associate one or more tags with an organization.</p>
  
  <p>Any help is appreciated, thanks!!</p>