Laravel migration change foreign id to be nullable
If you created a foreign key in your database which should have been nullable, you have to create a database migration to fix that.
To make your foreign key nullable, you first need to delete the Foreign Key constraint, then change the field to be nullable and then create the Foreign Key constraint again.
<?php
return new class extends Migration {
public function up(): void
{
Schema::table('retailers', function (Blueprint $table) {
$table->dropForeign('retailers_import_id_foreign');
$table->unsignedBigInteger('import_id')
->nullable(true)
->change();
$table->foreign('import_id')
->references('id')
->on('import_files')
->nullOnDelete()
->cascadeOnUpdate();
});
}
};