title: Laravel-How to insert into many to many related tables having already created foreign keys in mysql?
body: <p>When submitting form it throws the error:Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (portal_job.applicant_job, CONSTRAINT applicant_job_applicant_user_id_foreign FOREIGN KEY (applicant_user_id) REFERENCES applicants (id) ON DELETE CASCADE) (SQL: insert into applicant_job (applicant_user_id, created_at, job_id, updated_at) values (4, 2018-10-01 23:46:56, 2, 2018-10-01 23:46:56))</p>
  
  <p>How can I do an insert into two related tables? In this case, it would be many to many relationships. I don't know if creating the relationships on mysql beforehand might be causing the problem... if you need more information let me know please. I don't know what else to do because I've already try to do it using attach() and was of no use. Got the same error as well.</p>
  
  <p>This is the code being executed for the task. It first save data into applicants table and then it should do an insert into applicant_job which is the intermediate table but this part is no good.</p>
  
  <pre><code>public function store(Request $request, $id) {
      $this-&gt;validate($request, [
            'application_letter' =&gt; 'required'
        ]);
      $applicant = new Applicant;
      $applicant-&gt;application_letter= $request-&gt;input('application_letter');
      $applicant-&gt;job_id = $request-&gt;input('job');
      $applicant-&gt;state = 'pending';
      $applicant-&gt;user_id = auth()-&gt;user()-&gt;id;
  
      $applicant-&gt;save();
      $applicant-&gt;jobs()-&gt;sync([$id],false);
  
      return redirect("userdashboard");
    }
  </code></pre>
  
  <p>These are the models for the relationships between the tables</p>
  
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Applicant extends Model
  {
       protected $primaryKey = 'user_id';
  
       public $incrementing = false;
  
  
  
        function jobs() {
            return $this-&gt;belongsToMany('App\Models\Job')-&gt;withTimeStamps();
        }
  
        function user() {
          return $this-&gt;belongsTo('App\Models\User','user_id');
       }
  
       function profile() {
          return $this-&gt;hasOne('App\Models\Profile', 'profile');
       }
  }
  &lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Job extends Model
  {
  
  
    function category() {
     return $this-&gt;belongsTo('App\Models\JobCategory');
  }
  function user() {
     return $this-&gt;belongsTo('App\Models\User','user_id');
  }
  
  function applicants() {
       return $this-&gt;belongsToMany('App\Models\Applicant', 'applicant_job', 'job_id', 'applicant_user_id')-&gt;withTimeStamps();
   }
  }
  </code></pre>
  
  <p>These are the migrations with their respective foreign keys used for this part of the task.</p>
  
  <pre><code>Schema::create('applicants', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;text('application_letter');
              $table-&gt;string('state');
              $table-&gt;timestamps();
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;integer('job_id')-&gt;unsigned();
              $table-&gt;foreign('job_id')-&gt;references('id')
              -&gt;on('jobs')
              -&gt;onDelete('cascade');
              $table-&gt;foreign('user_id')-&gt;references('id')
                  -&gt;on('users')
                  -&gt;onDelete('cascade');
  
          });
    Schema::create('applicant_job', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('applicant_user_id')-&gt;unsigned();
              $table-&gt;integer('job_id')-&gt;unsigned();
              $table-&gt;timestamps();
              $table-&gt;foreign('applicant_user_id')-&gt;references('id')
                  -&gt;on('applicants')
                  -&gt;onDelete('cascade');
              $table-&gt;foreign('job_id')-&gt;references('id')
              -&gt;on('jobs')
              -&gt;onDelete('cascade');
          });
  Schema::create('jobs', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('title');
              $table-&gt;text('body');
              $table-&gt;decimal('payment');
              $table-&gt;integer('category_id')-&gt;unsigned();
              $table-&gt;string('position_type');
              $table-&gt;string('duration_project');
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;timestamps();
              $table-&gt;foreign('user_id')-&gt;references('id')
                  -&gt;on('users')
                  -&gt;onDelete('cascade');
                  $table-&gt;foreign('category_id')-&gt;references('id')
                      -&gt;on('job_category')
                      -&gt;onDelete('cascade');
  
          });
  </code></pre>
  
  <p>This is being done for a small job portal. Any more information you need, feel free to ask.</p>