1$table->unsignedBigInteger('user_id');
2$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
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});
1$table->foreign('column_name')->references('id')->on('table_name')->onDelete('cascade');
1Schema::table('posts', function (Blueprint $table) {
2 $table->unsignedBigInteger('user_id');
3
4 $table->foreign('user_id')->references('id')->on('users');
5});
1use Illuminate\Database\Schema\Blueprint;
2use Illuminate\Support\Facades\Schema;
3
4Schema::table('posts', function (Blueprint $table) {
5 $table->unsignedBigInteger('user_id');
6
7 $table->foreign('user_id')->references('id')->on('users');
8});
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