title: How to create a pivot table with related field other than id on Laravel 5.2
body: <p>I want to create (on Laravel 5.2) a pivot between two entities, let's assume that we have the entities User and Phone. In our world, a user can have several phones, and te phones can have several owners.</p>
  
  <p>For a reason, I must target the user's phone by it's serial number and the build year (let's assume that serials can't be identical on a same year).</p>
  
  <p>So I have my tables <code>users</code>, <code>phones</code> and<code>users_phones</code>.</p>
  
  <pre><code>===== Table users =====
  id
  username
  ...
  
  ===== Table phones ====
  id
  serial_numer
  build_year
  
  ===== Table users_phones ====
  user_id
  phone_serial
  phone_build_year
  </code></pre>
  
  <p>I have my relation on my model Phone :</p>
  
  <pre><code>public function users()
  {
      return $this-&gt;belongsToMany(
              User::class,
              'users_phones',
              'user_id',
              'phone_serial'
          )-&gt;withPivot('phone_build_year');
  }
  </code></pre>
  
  <p>When I wan't to add some entries like that :</p>
  
  <pre><code>$phone-&gt;users()-&gt;attach([$user-&gt;id =&gt; [
      'phone_build_year'  =&gt; $phone-&gt;build_year
  ]]);
  </code></pre>
  
  <p>The SQL request generated is the following one :</p>
  
  <pre><code>insert into `users_phones` (`user_id`, `phone_serial`, `phone_build_year`) values (1, 5, 2016)
  </code></pre>
  
  <p>My problem is that the <code>5</code> in the sql request is the phone id and not its serial. I didn't find how to precise to Laravel to use the field we chose (here the serial) rather than the id.</p>
  
  <p>Is it possible to do that, or am I condemned to create a model to manipulate my special pivot table ? I admit that I would appreciate if I could avoid that way.</p>