title: Error Many to Many Relationship in Laravel 5.8
body: <p>I got an error when I try to retrieve many to many relationship data in Laravel. The message of error is</p>
  <blockquote>
  <p>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from <code>jobs</code> where (not exists (select * from <code>job_types</code> inner join <code>job_types_jobs</code> on <code>job_types</code>.<code>id</code> = <code>job_types_jobs</code>.<code>job_types_id</code> where <code>jobs</code>.<code>id</code> = <code>job_types_jobs</code>.<code>jobs_id</code> and <code>job_types</code>.<code>deleted_at</code> is null) or exists (select * from <code>job_types</code> inner join <code>job_types_jobs</code> on <code>job_types</code>.<code>id</code> = <code>job_types_jobs</code>.<code>job_types_id</code> where <code>jobs</code>.<code>id</code> = <code>job_types_jobs</code>.<code>jobs_id</code> and <code>id</code> = 3 and <code>job_types</code>.<code>deleted_at</code> is null) and <code>name</code> LIKE %%) and <code>jobs</code>.<code>deleted_at</code> is null)</p>
  </blockquote>
  <p><strong>Table</strong>
  <a href="https://i.stack.imgur.com/h9Hnz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h9Hnz.png" alt="enter image description here" /></a></p>
  <p><strong>JobTypes.php</strong></p>
  <pre><code>public function jobs() // children  
  {
      // one type of job has many jobs
      return $this-&gt;belongsToMany('App\Jobs'); // id refer to jobs.id
  }
  </code></pre>
  <p><strong>Jobs.php</strong></p>
  <pre><code>public function job_types() // parent 
  {
      // one job only belongs to one type of job
      return $this-&gt;belongsToMany('App\jobTypes');
  }
  </code></pre>
  <p><strong>JobTypeController.php</strong></p>
  <pre><code>public function assignJob($id)
  {
      $jobType = \App\JobTypes::withTrashed()-&gt;find($id);
      $jobs = $jobType-&gt;jobs;
  
      return view('job-types.assign-job', compact(['jobType', 'jobs']));
  }
  </code></pre>
  <p><strong>View</strong></p>
  <pre><code>&lt;label for=&quot;name&quot;&gt;Job&lt;/label&gt;
                  &lt;select selected=&quot;selected&quot; multiple class=&quot;form-control {{ $errors-&gt;first('jobs') ? &quot;is-invalid&quot; : &quot;&quot; }}&quot; name=&quot;jobs[]&quot; id=&quot;jobs&quot; class=&quot;jobs&quot;&gt;&lt;/select&gt;
                  &lt;div class=&quot;invalid-feedback&quot;&gt;
                      {{ $errors-&gt;first('jobs') }}
                  &lt;/div&gt;
  </code></pre>
  <p><strong>Migarations</strong></p>
  <pre><code>Schema::create('job_types', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;timestamps();
      });
  
      Schema::create('jobs', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;timestamps();
      });
  
      Schema::create('jobs_job_types', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('jobs_id')-&gt;unsigned()-&gt;index();
          $table-&gt;integer('job_types_id')-&gt;unsigned()-&gt;index();
  
          $table-&gt;foreign('jobs_id')-&gt;references('id')-&gt;on('jobs')-&gt;onDelete('cascade');
          $table-&gt;foreign('job_types_id')-&gt;references('id')-&gt;on('job_types')-&gt;onDelete('cascade');
      });
  </code></pre>