In this post I will outline migrating your existing subscription data from Laravel Cashier version 5 to version 6. In version 6, Cashier got a nice update to handle multiple subscriptions but there is not guide to migrate your data. Fear not, it’s actually pretty easy!

The first thing you should do is backup your data! Also please read through the docs located at https://laravel.com/docs/5.2/billing. I’m not going to cover the code changes in this post but I will show you how to migrate your data.

Step 1

Update your package to use version 6 of cashier and run composer update. Also follow the setup guide listed in the Laravel docs.

"laravel/cashier": "~6.0"

Step 2

Add and run the migrations as outlined in the docs.

Schema::table('users', function ($table) { $table->string('stripe_id')->nullable(); $table->string('card_brand')->nullable(); $table->string('card_last_four')->nullable(); }); Schema::create('subscriptions', function ($table) { $table->increments('id'); $table->integer('user_id'); $table->string('name'); $table->string('stripe_id'); $table->string('stripe_plan'); $table->integer('quantity'); $table->timestamp('trial_ends_at')->nullable(); $table->timestamp('ends_at')->nullable(); $table->timestamps(); });

As you can see the biggest change here is that we have a new “subscriptions” table to hold our stripe data. The user table only holds the customer id and card data. At this point our user table still has Cashier v5 columns and data. The next step is to actually copy the data to the new “subscriptions” table.

Step 3

Update “users” table column data. You can either copy the data or rename the column. For simplicity, I’m just going to copy the data (we will drop the old column later anyway). Here is the query to copy the data.

UPDATE `users` set `card_last_four` = `last_four`

Step 4

Copy the stripe data from the “users” table into the “subscriptions” table. We will run another query here to copy the data into our new table.

INSERT INTO `subscriptions` (`user_id`, `name`, `stripe_id`, `stripe_plan`, `quantity`, `trial_ends_at`, `ends_at`, `created_at`, `updated_at`) SELECT `id`, 'membership', `stripe_subscription`, `stripe_plan`, '1', `trial_ends_at`, `subscription_ends_at`, `created_at`, `updated_at` FROM `users` WHERE `users`.`stripe_active` = '1'

Easy right? Your data is all setup now and ready for cashier version 6. Before continuing to the next step, I highly recommend you verify your data was copied correctly.

Step 5

The last step is to actually remove version 5 columns (since we no longer need them)

Schema::table('users', function (Blueprint $table) { $table->dropColumn(['stripe_active', 'stripe_subscription', 'stripe_plan', 'trial_ends_at', 'subscription_ends_at', 'last_four']); });

Profit!