title: Laravel - how to use belongsToMany relationship using second index and not primary key
body: <h3>App overview</h3>
  
  <p>I'm learning Laravel and my dummy app allows users to log issues against projects. A project can have many issues and an issue can have many tags assigned to it.</p>
  
  <p>I'm currently working on <code>projects/show.blade.php</code> which outputs all issues associated with a project in an HTML table with columns for <code>name</code> and <code>tags</code>.</p>
  
  <h3>Issue</h3>
  
  <p>I've setup 'belongs to many' relationships so that my <code>issues</code>, <code>tags</code> and <code>issues_tags</code> (intermediary) tables can talk to each other but I've run into a problem whereby tags are being output against the wrong issues.</p>
  
  <p>The problem stems from the fact that my <code>issues</code> table (by design) includes an <code>item_id</code> column, which is an index but not the primary key. I use <code>item_id</code> for routing/display on the frontend because I wanted the issue ID that users see to start from 1 for every new project added instead of being sequential and everyone knowing how many there are in the database.</p>
  
  <p>For example, tags assigned to issue #3 are called using <code>issues.id</code> and not <code>issues.item_id</code>.</p>
  
  <h3>Sample data</h3>
  
  <p><a href="https://i.stack.imgur.com/YKVUn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YKVUn.png" alt="Sample data"></a></p>
  
  <p><strong>What I want:</strong> Tag_id's 1, 2 and 3 to be assigned to issue 3 where <code>issues.item_id</code> is the index upon which to match. E.g.</p>
  
  <p><a href="https://i.stack.imgur.com/X9cLq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X9cLq.png" alt="Correct result"></a></p>
  
  <p><strong>What I am getting:</strong> Tag_id's 1, 2 and 3 are assigned to issue 3 but are retrieved using <code>issues.id</code> as the index to match against. E.g.</p>
  
  <p><a href="https://i.stack.imgur.com/ludlD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ludlD.png" alt="Incorrect result"></a></p>
  
  <h3>Code</h3>
  
  <p><strong>Issues migration</strong></p>
  
  <pre><code>    Schema::create('issues', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('item_id')-&gt;index()-&gt;unsigned();
          $table-&gt;integer('project_id')-&gt;unsigned();
          $table-&gt;string('name');
          $table-&gt;longText('description');
          $table-&gt;tinyInteger('status')-&gt;default('1');
          $table-&gt;integer('created_by');
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p><strong>Tags migration</strong></p>
  
  <pre><code>Schema::create('tags', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;string('name');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p><strong>Issues tags migration</strong></p>
  
  <pre><code>Schema::create('issues_tags', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;integer('tag_id')-&gt;unsigned()-&gt;index();
      $table-&gt;foreign('tag_id')-&gt;references('id')-&gt;on('tags');
      $table-&gt;integer('issue_id')-&gt;unsigned()-&gt;index();
      $table-&gt;foreign('issue_id')-&gt;references('item_id')-&gt;on('issues');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p><strong>Issue model</strong></p>
  
  <pre><code>public function tags()
  {
      return $this-&gt;belongsToMany('App\Tag', 'issues_tags');
  }
  </code></pre>
  
  <p><strong>Tag model</strong></p>
  
  <pre><code>public function issues()
  {
      return $this-&gt;belongsToMany('App\Issue', 'issues_tags', 'issue_id')-&gt;withPivot('item_id');
  }
  </code></pre>
  
  <p><strong>View (projects/show.blade.php)</strong></p>
  
  <pre><code>  &lt;table&gt;
      &lt;tr&gt;
        &lt;th&gt;ID&lt;/th&gt;
        &lt;th&gt;Item ID&lt;/th&gt;
        &lt;th&gt;Issue&lt;/th&gt;
        &lt;th&gt;Tags&lt;/th&gt;
      &lt;/tr&gt;
      @foreach($issues as $issue)
      &lt;tr&gt;
        &lt;td&gt;{{ $issue-&gt;id }}&lt;/td&gt;
        &lt;td&gt;{{ $issue-&gt;item_id }}&lt;/td&gt;
        &lt;td&gt;{{ $issue-&gt;name }}&lt;/td&gt;
        &lt;td&gt;
          @foreach($issue-&gt;tags as $tag)
            &lt;span class="badge badge-primary"&gt;{{ $tag-&gt;name }}&lt;/span&gt;
          @endforeach
        &lt;/td&gt;
      &lt;/tr&gt;
      @endforeach
    &lt;/table&gt;
  </code></pre>
  
  <hr>
  
  <h3>Question</h3>
  
  <p>How can I use <code>issues.item_id</code> instead of <code>issues.id</code> when using a belongsToMany relationship in Laravel?</p>