You can write DB statements in your migrations. So when you make changes to the schema you can simply write queries to move data where you want it. Here is an example migration from when I moved to the new version of Laravel Cashier and had to migrate some data:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class UpdateStripe extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('clinics', function (Blueprint $table) {
$table->string('card_brand')->nullable();
$table->renameColumn('last_four', 'card_last_four');
});
Schema::create('subscriptions', function (Blueprint $table) {
$table->increments('id');
$table->integer('clinic_id');
$table->string('name');
$table->string('stripe_id');
$table->string('stripe_plan');
$table->integer('coupon_id')->nullable();
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
DB::statement(
"INSERT INTO `subscriptions` (`clinic_id`, `name`, `stripe_id`, `stripe_plan`, `coupon_id`, `quantity`, `trial_ends_at`, `ends_at`, `created_at`, `updated_at`)
SELECT `id`, 'membership', `stripe_subscription`, `stripe_plan`, `coupon_id`, '1', `trial_ends_at`, `subscription_ends_at`, `created_at`, `updated_at`
FROM `clinics` WHERE `stripe_active` = '1'"
);
Schema::table('clinics', function (Blueprint $table) {
$table->dropColumn(['stripe_active', 'stripe_subscription', 'stripe_plan', 'trial_ends_at', 'subscription_ends_at', 'coupon_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Rebuild the clinics table
Schema::table('clinics', function (Blueprint $table) {
$table->tinyInteger('stripe_active')->default(0);
$table->string('stripe_subscription')->nullable();
$table->string('stripe_plan', 25)->nullable();
$table->integer('coupon_id')->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('subscription_ends_at')->nullable();
$table->dropColumn('card_brand');
$table->renameColumn('card_last_four', 'last_four');
});
// Insert the data from the subscriptions table.
DB::statement(
'UPDATE clinics, subscriptions
set clinics.stripe_active = 1,
clinics.stripe_subscription = subscriptions.stripe_id,
clinics.stripe_plan = subscriptions.stripe_plan,
clinics.coupon_id = subscriptions.coupon_id,
clinics.trial_ends_at = subscriptions.trial_ends_at,
clinics.subscription_ends_at = subscriptions.ends_at
WHERE clinics.id = subscriptions.clinic_id'
);
Schema::drop('subscriptions');
}
}