Du skal oprette kolonnen relateret til fremmednøglen:
class CreateAreasTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Creates the cemeteries table
Schema::create('areas', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('region_id')->unsigned();
$table->foreign('region_id')->references('id')->on('regions');
$table->string('name', 160)->unique();
$table->timestamps();
});
}
}
Nogle gange (afhænger af din databaseserver) bliver du nødt til at oprette dine fremmednøgler i to trin:
class CreateAreasTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Create the table and the foreign key column
Schema::create('areas', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('region_id')->unsigned();
$table->string('name', 160)->unique();
$table->timestamps();
});
// Create the relation
Schema::tabe('areas', function($table)
{
$table->foreign('region_id')->references('id')->on('regions');
});
}
}