1Schema::table('posts', function (Blueprint $table) {
2 $table->unsignedBigInteger('user_id');
3
4 $table->foreign('user_id')->references('id')->on('users');
5});
6OR
7Schema::table('posts', function (Blueprint $table) {
8 $table->foreignId('user_id')->constrained();
9});
1Schema::table('posts', function (Blueprint $table) {
2 $table->unsignedBigInteger('user_id');
3
4 $table->foreign('user_id')->references('id')->on('users');
5});
1//Note : Before Renaming Foreign, You Must Need To Delete Old Foreign And Assign New One
2class RenameColumn extends Migration
3{
4
5 public function up()
6 {
7 Schema::table('holidays', function(Blueprint $table) {
8 $table->dropForeign('holidays_account_id_foreign');
9 $table->renameColumn('account_id ', 'engagement_id');
10
11 $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
12 });
13 }
14
15 public function down()
16 {
17 Schema::table('holidays', function(Blueprint $table) {
18 $table->dropForeign('holidays_engagement_id_foreign');
19 $table->renameColumn('account_id ', 'engagement_id');
20
21 $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
22 });
23 }
24}
25
1$table->foreignId('user_id')
2 ->constrained("users") <- // You don't need to specify table if it matched laravel naming conventions.
3 ->onUpdate('cascade')
4 ->onDelete('cascade');