title: Laravel8 Many To Many change id for custom attribute
body: <p>I created a relationship <code>many-to-many</code> between <code>Faculties</code> and <code>Departments</code>(im Working in a prototype timetable problem). For a petition, these tables need a validation code <code>vcode</code>. The problems are in the relationship of this, I don't know why the relationship recognize only the <code>id</code>´s tables for the eloquent query and not the <code>vcode</code>.</p>
  <p>attached my migrations and model relationship.</p>
  <p>Migration Faculties Table</p>
  <pre><code>public function up()
  {
      Schema::create('faculties', function (Blueprint $table) {
          $table-&gt;id();
          $table-&gt;unsignedBigInteger('vcode')-&gt;index()-&gt;unique();
          $table-&gt;string('code');
          $table-&gt;string('name');
          $table-&gt;string('website');
          $table-&gt;string('email');
          $table-&gt;string('phone');
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  <p>Relationshop on Faculty Model</p>
  <pre><code>public function departments()
  {
      return $this-&gt;belongsToMany(Department::class, 'faculty_departments','faculty_vcode','department_vcode');
  } 
  </code></pre>
  <p>Migration Departments Table</p>
  <pre><code>public function up()
  {
      Schema::create('departments', function (Blueprint $table) {
          $table-&gt;id();
          $table-&gt;unsignedBigInteger('vcode')-&gt;index()-&gt;unique();
          $table-&gt;string('code');
          $table-&gt;string('name');
          $table-&gt;string('phone');
          $table-&gt;string('email');
          $table-&gt;string('website');
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  <p>Relationshop on Department Model</p>
  <pre><code>public function faculties()
  {
      return $this-&gt;belongsToMany(Faculty::class, 'faculty_departments', 'department_vcode','faculty_vcode',);
  }
  </code></pre>
  <p>Migration FacultyDepartments (Relationship)</p>
  <pre><code>public function up()
  {
      Schema::create('faculty_departments', function (Blueprint $table) {
          $table-&gt;id();
          $table-&gt;unsignedBigInteger('faculty_vcode')-&gt;nullable()-&gt;index();
          $table-&gt;foreign('faculty_vcode')
                -&gt;references('vcode')
                -&gt;on('faculties')
                -&gt;cascadeOnUpdate()
                -&gt;nullOnDelete();
          $table-&gt;unsignedBigInteger('department_vcode')-&gt;nullable()-&gt;index();
          $table-&gt;foreign('department_vcode')
                -&gt;references('vcode')
                -&gt;on('departments')
                -&gt;cascadeOnUpdate()
                -&gt;nullOnDelete();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  <p><strong>I provocate a error for get the query and tested the error (getting id´s table)</strong>.</p>
  <p><code>faculty::with('departments')-&gt;get()</code> gives me:</p>
  <pre class="lang-sql prettyprint-override"><code>select `departments`.*, `faculty_departments`.`faculty_vcode` as `pivot_faculty_vcode`, `faculty_departments`.`department_vcode` as `pivot_department_vcode` 
  from `departments` 
  inner join `faculty_departments` on `departments`.`id` = `faculty_departments`.`department_vcode` 
   
  where `faculty_departments`.`faculty_vcode` in (1, 2, 3, 4, 5, 6, 7)
  </code></pre>