Created
July 14, 2020 14:28
-
-
Save jesseschutt/303327ed57ecb7cc3fee592230a2b4a7 to your computer and use it in GitHub Desktop.
Renaming a column that is a foreign key in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class ConvertCustomerIdToUserIdOnSubscriptionsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::table('subscriptions', function(Blueprint $table) { | |
$table->dropForeign(['customer_id']); | |
$table->renameColumn('customer_id', 'user_id'); | |
$table->foreign('user_id') | |
->references('id') | |
->on('users') | |
->onDelete('cascade'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::table('subscriptions', function(Blueprint $table) { | |
$table->dropForeign(['user_id']); | |
$table->renameColumn('user_id', 'customer_id'); | |
$table->foreign('customer_id') | |
->references('id') | |
->on('users') | |
->onDelete('cascade'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment