How to migrate data from Laravel Cashier 5 to 6

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.

Step 2
Add and run the migrations as outlined in the docs.

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.

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.

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)

Profit!

, ,

  • http://www.harisbacic.com/ Haris

    Thanks for this article. Are you sure ‘trial_ends_at’ can be removed from users table? It shows that column as needed in Laravel documentation seen here:
    https://laravel.com/docs/5.2/billing

  • http://pixney.com/ William Åström

    Thanks man. Came back to an old project and this came handy!

Powered by WordPress. Designed by Woo Themes