Before Using foreignIdFor Function
Before having the foreignIdFor function, the code to define a foreign key is quite long and prote for error.
<?php Schema::table('posts', function (Blueprint $table) { $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users'); });
After Using foreignIdFor Function
Now you can update the code above with the use of "foreignId" function.
<?php Schema::table('posts', function (Blueprint $table) { $table->foreignId('user_id')->constrained(); });
<?php Schema::table('posts', function (Blueprint $table) { $table->foreignIdFor(User::class)->constrained(); });
Leave a reply