title: How to create a Pivot table in Laravel
body: <p>I have a sponsorship app I'm creating and trying to get a pivot table working between my users and kids, but when I have a user sponsor a kid, the pivot table <code>kid_user</code> is not populated with the <code>kid_id</code> or the <code>user_id</code>. Not sure what I'm missing.</p>
  
  <ul>
  <li>A Kid can have as many users (sponsors) as their slots allow them.</li>
  <li>Users can sponsor a kid, multiple kids or the same kid multiple times
  if needed.</li>
  </ul>
  
  <p><strong>Here is my <code>kid_user</code> pivot table:</strong></p>
  
  <pre><code>public function up()
  {
      Schema::create('kid_user', function (Blueprint $table) {
          $table-&gt;integer('kid_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('kid_id')-&gt;references('id')-&gt;on('kids')-&gt;onDelete('cascade');
          $table-&gt;integer('user_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
          $table-&gt;primary(['kid_id', 'user_id']);
      });
  }
  </code></pre>
  
  <p><strong>Then on the User model:</strong> The user can have many kids...</p>
  
  <pre><code>public function kid()
  {
      return $this-&gt;hasMany(Kid::class);
  }
  </code></pre>
  
  <p><strong>Then on the Kid model:</strong> The kids can have many users...</p>
  
  <pre><code>  public function user() {
        //return $this-&gt;belongsTo(User::class); (Tried this..)
          return $this-&gt;hasMany(User::class);
    }
  </code></pre>
  
  <p><strong>Here are the tables I'm using.</strong> (Only relevant info is included in the tables)</p>
  
  <pre><code>+---------------------+
  | Tables_in_Database  |
  +---------------------+
  | kid_user            |
  | kids                |
  | users               |
  +---------------------+
  
  +-----------------------------+
  |        users table          |
  +----+------------+-----------+
  | id | first_name | last_name |
  +----+------------+-----------+
  |  1 | John       | Smith     |
  |  2 | Jane       | Doe       |
  +----+------------+-----------+
  
  +-----------------------------+
  |        kids table           |
  +----+------------+-----------+-------+
  | id | first_name | last_name | slots |
  +----+------------+-----------+-------+
  |  1 | Bobby      | Little    |   3   | -&gt; Can be sponsored 3 times
  +----+------------+-----------+-------+
  
  +--------------------+
  |   kid_user table   | THE BELOW RESULTS ARE WHAT I'M LOOKING FOR.
  +--------------------+
  | *kid_id | *user_id | * = Primary Key
  +---------+----------+
  |    1    |     1    | -&gt; Sponsored 1st Slot
  +---------+----------+
  |    1    |     2    | -&gt; Sponsored 2nd Slot
  +---------+----------+
  |    1    |     1    | -&gt; Sponsored 3rd Slot
  +---------+----------+
  </code></pre>
  
  <p>So when a User sponsors a Kid. I would like the <code>kid_id</code> &amp; <code>user_id</code> to be entered into the <code>kid_user</code> pivot table. Any help would be appreciated.</p>