title: Laravel inserting into a three-way pivot table
body: <h2>Summary</h2>
  <p>I am building a music discovery service. My question is: <strong>How do I insert data into the three-way pivot table</strong> <em>Tag_Track_User</em> <strong>?</strong></p>
  <h2>Schema</h2>
  <p>I have this schema seen <a href="http://www.laravelsd.com/share/WNmzz8" rel="noreferrer">here at LaravelSD</a></p>
  <p>It comprises of six main tables (and a few others):
  <strong>Artists, Albums, Tracks, Tags, Users and Tag_Track_User</strong></p>
  <p><img src="https://i.stack.imgur.com/8WWDF.jpg" alt="Schema" /></p>
  <p>The <strong>Artists-&gt;Albums-&gt;Tracks</strong> relationship is straightforward and as you'd expect.</p>
  <p><strong>Tags, Tracks and Users</strong> all relate to one-another as <strong>no two can exist without the third</strong>.</p>
  <h2>Relationships</h2>
  <p><strong>Artists</strong> <em>hasMany()</em> <strong>Albums</strong></p>
  <p><strong>Albums</strong> <em>hasMany()</em> <strong>Tracks</strong> and <em>belongsTo()</em> an <strong>Artist</strong></p>
  <p><strong>Tracks</strong> <em>belongsTo()</em> <strong>Albums</strong></p>
  <p><strong>Tracks</strong> <em>belongsToMany()</em> <strong>Tags</strong> and <em>belongsToMany()</em> an <strong>Users</strong></p>
  <p><strong>Tags</strong> <em>belongsToMany()</em> <strong>Tracks</strong> and <em>belongsToMany()</em> an <strong>Users</strong></p>
  <p><strong>Users</strong> <em>belongsToMany()</em> <strong>Tags</strong> and <em>belongsToMany()</em> an <strong>Tracks</strong></p>
  <h2>Models</h2>
  <p><strong>User model</strong></p>
  <pre><code>public function tags()
  {
      return $this-&gt;belongsToMany('Tag', 'tag_track_user', 'user_mdbid', 'tag_mdbid')-&gt;withPivot('track_mdbid');
  }
  
  /**
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function tracks()
  {
      return $this-&gt;belongsToMany('Track', 'tag_track_user', 'user_mdbid', 'track_mdbid')-&gt;withPivot('tag_mdbid');
  }
  </code></pre>
  <p>The <strong>Tag</strong> and <strong>Track</strong> model contain the same respective relationships.</p>
  <h2>Question</h2>
  <p>So my question is:</p>
  <p>How do I insert data into the <em>Tag_Track_User</em> table? The <em>tag_track_user</em> table is a 3-way pivot table cointaining information about tracks that users have tagged.</p>
  <p>You have to be logged in to tag a track (which means I have access to the user’s ID). The tracks ID is accessed as I am displaying it on the page where the form is contained. The tag on the other hand; if it already exists in the tags table, I want to get it’s ID and re-use that (as they are unique), if not, I want to create it, assign it an ID and insert that into the tag_track_user_table.</p>
  <ol>
  <li>I need to check whether the <em>Tag</em> exists</li>
  <li>If it does, get it's ID</li>
  <li>Insert data into the <em>Tag_Track_User</em> table</li>
  </ol>
  <h2>Thank you</h2>
  <p>Any help I receive on this, is greatly appreciated.</p>