06.10.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Database: Migrations 548<br />

1 Schema::table('posts', function ($table) {<br />

2 $table->integer('user_id')->unsigned();<br />

3<br />

4 $table->foreign('user_id')->references('id')->on('users');<br />

5 });<br />

You may also specify the desired action for the “on delete” and “on update” properties of the<br />

constraint:<br />

1 $table->foreign('user_id')<br />

2 ->references('id')->on('users')<br />

3 ->onDelete('cascade');<br />

To drop a foreign key, you may use the dropForeign method. Foreign key constraints use the same<br />

naming convention as indexes. So, we will concatenate the table name and the columns in the<br />

constraint then suffix the name with “_foreign”:<br />

1 $table->dropForeign('posts_user_id_foreign');<br />

Or you may pass an array value which will automatically use the conventional constraint name<br />

when dropping:<br />

1 $table->dropForeign(['user_id']);<br />

You may enable or disable foreign key constraints within your migrations by using the following<br />

methods:<br />

1 Schema::enableForeignKeyConstraints();<br />

2<br />

3 Schema::disableForeignKeyConstraints();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!