title: How do I create A joined table in Laravel Migration?
body: <p>I have a users table with the following columns with User model has one to many relationship with phone model.</p>
  
  <p><strong>create_users_table.php</strong></p>
  
  <pre><code>public function up()
  {
      Schema::create('users', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;string('name');
          $table-&gt;string('email')-&gt;unique();
          $table-&gt;timestamp('email_verified_at')-&gt;nullable();
          $table-&gt;string('password');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>Then I have a phones table, which has a foreign key of user_id
  <strong>create_phones_table.php</strong></p>
  
  <pre><code>public function up()
  {
      Schema::create('phones', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;string('phone');
          $table-&gt;timestamps();
  
          $table-&gt;foreign('user_id')
          -&gt;references('id')
          -&gt;on('users')
          -&gt;onDelete('cascade');
      });
  }
  </code></pre>
  
  <p><strong>My User Model</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use App\Phone;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  
  class User extends Authenticatable
  {
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'email', 'password',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
  
      /**
       * Define Relationship
       */
  
      public function phones()
      {
          return $this-&gt;hasMany(Phone::class);
      }
  }
  </code></pre>
  
  <p><strong>My Phone Model</strong></p>
  
  <pre><code>&lt;?php
  
      namespace App;
  
      use App\User;
      use Illuminate\Database\Eloquent\Model;
  
  class Phone extends Model
  {
      protected $table = 'phones';
      protected $fillable = ['phone' , 'user_id'];
  
      public function user()
      {
          return $this-&gt;belongsTo(User::class);
      }
  }
  </code></pre>
  
  <p>I would like to have a third table in my database call phone_user_table.php. Something like a pivot table where I have joined the user table and phone table where I can see all the records. This is my code where I attempt to join the table.</p>
  
  <p><strong>create_phone_user.php</strong></p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Support\Facades\Schema;
  
  class CreatePhoneUser extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('phone_user', function (Blueprint $table) {
  
              $table-&gt;unsignedBigInteger('user_id')-&gt;unsigned();
              $table-&gt;unsignedBigInteger('phone_id')-&gt;unsigned();
  
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
              $table-&gt;foreign('phone_id')-&gt;references('id')-&gt;on('phones')-&gt;onDelete('cascade');
  
              $table-&gt;timestamps();
  
              $table 
              -&gt;join('users', 'users.id', '=', 'phone_user.user_id')
              -&gt;join('phones', 'phones.id', '=', 'phone_user.phone_id')
              -&gt;select('phone_user.*', 'users.name', 'phones.phone')
              -&gt;get();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('phone_user');
      }
  }
  </code></pre>
  
  <p>However, it seems to be giving Base Table phones already exist.</p>
  
  <p>Appreciate all the help given.</p>
  
  <p>Thank you.</p>