title: How can I attach the user id and role id to the pivot table in laravel using attach function?
body: <p><code>RegisterController.php</code></p>
  <pre class="lang-php prettyprint-override"><code>    protected function create(array $data)
      {
          $role = Role::where('name', 'customer')-&gt;first();
  
          $user = User::create([
              'name' =&gt; $data['name'],
              'email' =&gt; $data['email'],
              'password' =&gt; Hash::make($data['password']),
              'remember_token' =&gt; Str::random(60),
          ]);
  
          $user-&gt;role()-&gt;attach(['user_id' =&gt; $user-&gt;id, 'role_id' =&gt; $role-&gt;id]);
  
          return $user;
      }
  </code></pre>
  <blockquote>
  <p>On above code segement I am trying to <code>attach</code> the <code>user_id</code> and <code>role_id</code> to the <code>pivot</code> table which is named as <code>role_user</code> at the time of <strong>User Registration</strong></p>
  </blockquote>
  <p><code>User.php</code></p>
  <pre class="lang-php prettyprint-override"><code>public function role()
  {
     return $this-&gt;belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
  }
  </code></pre>
  <blockquote>
  <p>In above code segment is the relationship between <code>User</code> and <code>Role</code></p>
  </blockquote>
  <p>The error I am getting.</p>
  <blockquote>
  <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>deal_ocean</code>.<code>role_user</code>, CONSTRAINT <code>role_user_role_id_foreign</code> FOREIGN KEY (<code>role_id</code>) REFERENCES <code>roles</code> (<code>id</code>) ON DELETE CASCADE) (SQL: insert into <code>role_user</code> (<code>role_id</code>, <code>user_id</code>) values (5, 5), (4, 5))</p>
  </blockquote>
  <p><strong>For Reference</strong>: Below is the tables I have created.</p>
  <pre class="lang-php prettyprint-override"><code>         Schema::create('users', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;unsignedBigInteger('role_id')-&gt;default(4);
              $table-&gt;string('name');
              $table-&gt;string('email')-&gt;unique();
              $table-&gt;string('photo')-&gt;nullable();
              $table-&gt;timestamp('email_verified_at')-&gt;nullable();
              $table-&gt;string('password');
              $table-&gt;longText('cartitems')-&gt;nullable();
              $table-&gt;longText('wishlist')-&gt;nullable();
              $table-&gt;unsignedBigInteger('discount')-&gt;default(0);
              $table-&gt;rememberToken();
              $table-&gt;timestamps();
  
              $table-&gt;foreign('role_id')-&gt;references('id')-&gt;on('roles')-&gt;onDelete('cascade');
          });
  </code></pre>
  <blockquote>
  <p>Above table is for <code>users</code> table.</p>
  </blockquote>
  <pre class="lang-php prettyprint-override"><code>       Schema::create('roles', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;string('name');
              $table-&gt;string('display_name');
              $table-&gt;timestamps();
          });
  </code></pre>
  <blockquote>
  <p>Above table is for <code>roles</code> table.</p>
  </blockquote>
  <pre class="lang-php prettyprint-override"><code>       Schema::create('role_user', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;unsignedBigInteger('user_id');
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
  
              $table-&gt;unsignedBigInteger('role_id');
              $table-&gt;foreign('role_id')-&gt;references('id')-&gt;on('roles')-&gt;onDelete('cascade');
  
              $table-&gt;timestamps();
          });
  </code></pre>
  <blockquote>
  <p>Above table is for <code>role_user</code> table.</p>
  </blockquote>