Skip to main content

Foreign Keys

There are two ways to reference foreign keys. This example uses a Parent-Child relationship and shows what to put in the child migration.

Long way with the option to use different keys:

$table->integer('child_id')->unsigned()->index();
$table->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade');
            

Short hand single line:

table->foreignId('parent_id')->constrained()->cascadeOnDelete();

See further information here: Foreign Key Constraints