Skip to content

Instantly share code, notes, and snippets.

@jesseschutt
Created July 14, 2020 14:28
Show Gist options
  • Save jesseschutt/303327ed57ecb7cc3fee592230a2b4a7 to your computer and use it in GitHub Desktop.
Save jesseschutt/303327ed57ecb7cc3fee592230a2b4a7 to your computer and use it in GitHub Desktop.
Renaming a column that is a foreign key in Laravel
<?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