$table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users');
In this example, we say that the user_id column refers to the id column in the users table. Be sure to first create a foreign key column! The column user_id is declared unsigned because it cannot have a negative value.
You can also specify parameters for the "on delete" and "on update" restrictions:
$table->foreign('user_id') ->references('id')->on('users') ->onDelete('cascade');
To remove a foreign key, you can use the dropForeign method. A similar naming convention is used for foreign keys that are used for other indexes:
$table->dropForeign('posts_user_id_foreign');
If you're fairly new to Laravel and Eloquent , try Laravel From Scratch available on Laracasts. This is a great guide for beginners.
source share