xxxxxxxxxx
$table->foreignIdFor(FileEntry::class);
xxxxxxxxxx
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
xxxxxxxxxx
//Note : Before Renaming Foreign, You Must Need To Delete Old Foreign And Assign New One
class RenameColumn extends Migration
{
public function up()
{
Schema::table('holidays', function(Blueprint $table) {
$table->dropForeign('holidays_account_id_foreign');
$table->renameColumn('account_id ', 'engagement_id');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
}
public function down()
{
Schema::table('holidays', function(Blueprint $table) {
$table->dropForeign('holidays_engagement_id_foreign');
$table->renameColumn('account_id ', 'engagement_id');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
}
}
xxxxxxxxxx
$table->foreign('column_name')->references('id')->on('table_name')->onDelete('cascade');
xxxxxxxxxx
$table->foreignId('post_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
xxxxxxxxxx
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddForeignKeyToTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('your_table_name', function (Blueprint $table) {
$table->foreign('foreign_key_column')->references('referenced_column')->on('referenced_table')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('your_table_name', function (Blueprint $table) {
$table->dropForeign(['foreign_key_column']);
});
}
}