title: Foreign Key Constraint in Laravel Migration
body: <p>In Laravel 4, how can I add a foreign key constraint in a migration?</p>
  
  <p>In the migration for <code>mytable</code> (which references <code>foreigntable</code>):</p>
  
  <pre><code>// add Column
  $table
      -&gt;string( 'foreigntable_id', 6 );
  
  // add FK
  $table
      -&gt;foreign( 'foreigntable_id' )
      -&gt;references( 'id' )
      -&gt;on( 'foreigntable' );
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>[Exception]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'mydb.#sql-1a24_2
   1a' (errno: 150) (SQL: alter table `mytable` add constraint 
  mytable_foreigntable_id_foreign foreign key (`foreigntable_id`) references 
  `foreigntable` (`id`)) (Bindings: array (
  ))
  </code></pre>
  
  <p>I assume the problem is that <code>foreigntable</code> does not exist when MySQL tries to add the foreign key constraint to <code>mytable</code> (because the migration that creates <code>foreigntable</code> will only be run <em>after</em> the migration for <code>mytable</code> was finished). </p>
  
  <p>How can I get around this problem?</p>