title: Laravel Foreign key Referencing table in External database?
body: <p>I'm new to Laravel and databases in general. I'm writing a web application for student evaluations. I have an existing MySQL database that contains everything I need already; however, I am using Laravel's auth user table and trying to add a foreign key that references a Teacher table in the MySQL database. I keep getting the following error...</p>
  
  <pre><code>php artisan migrate:fresh
  </code></pre>
  
  <blockquote>
    <p>In Connection.php line 664:<br>
    SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
    (SQL: alter table <code>users</code> add constraint <code>users_teacher_id_foreign</code> 
    foreign key (<code>teacher_id</code>) references <code>p4j</code>.<code>teacher</code> (<code>teacher_id</code>)) </p>
    
    <p>In Connection.php line 458: SQLSTATE[HY000]: General error: 1215 Cannot
    add foreign key constraint</p>
  </blockquote>
  
  <p>My code is as follows...</p>
  
  <p><strong>config/database.php</strong></p>
  
  <pre><code>'mysql' =&gt; [
      'driver' =&gt; 'mysql',
      'host' =&gt; env('DB_HOST', '127.0.0.1'),
      'port' =&gt; env('DP_PORT', '3306'),
      'database' =&gt; env('DB_DATABASE', 'p4j'),
      'username' =&gt; env('DB_USERNAME', 'root'),
      'password' =&gt; env('DP_PASSWORD', '**mypassword**'),
      'charset' =&gt; 'utf8',
      'collation' =&gt; 'utf8_unicode_ci',
      'prefix' =&gt; '',
  ],
  </code></pre>
  
  <p><strong>database/migrations/2014_10_12_000000_create_users_table.php</strong></p>
  
  <pre><code>public function up()
  {
      Schema::create('users', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;string('email')-&gt;unique();
          $table-&gt;string('password');
          $table-&gt;integer('teacher_id')-&gt;unsigned();
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  
      Schema::table('users', function (Blueprint $table) {
            $table-&gt;foreign('teacher_id')-&gt;references('teacher_id')-&gt;on('p4j.teacher');
      });
  }
  </code></pre>
  
  <p><strong>app/User.php</strong></p>
  
  <pre><code>protected $fillable = [
      'name', 'email', 'password', 'teacher_id'
  ];
  
  /**
   * The attributes that should be hidden for arrays.
   *
   * @var array
   */
  protected $hidden = [
      'password', 'remember_token',
  ];
  </code></pre>