Database Orders

After a couple thousand (or maybe a couple hundred) of orders, your site can start to feel slow when storing orders as entries. However, there is a solution! Using a database.

But wait.. you say. Isn't Statamic a flat-file CMS? The answer is yes but when your site starts to scale, you can run into bottlenecks. Databases have proven to be able to scale well and are easy for querying.

When should I switch to a database?

  • You have a few hundred/thousand orders and you're starting to see performance suffer
  • You need to do some kind of complicated queries against your orders
  • You want to keep your orders out of version control

How it works

So, instead of your orders & customers living as entries in your content folder, they will live in your database. Simple Commerce uses Eloquent in order to talk with the database (Eloquent is a Laravel thing).

Switching to a database


The below steps assume you already have a database setup.

I've written a few command to make the process of switching your site to a database as painless as possible.

First, you'll want to run the 'switch' command which will update your Simple Commerce config file & install Runway so you can manage your orders in the Control Panel.

1php please sc:switch-to-database

The above command will publish some database migrations, you'll need to run the php artisan migrate for those migrations to take place.

Then, to move over any existing order & customer entries, you can run the provided 'migrate' command. It may be a good idea to run this per environment (eg. local, staging, production).

1php please sc:migrate-to-database

One thing worth noting is that the above command will not delete the order/customer entries or the collections. That's something you should do yourself after you're satisfied with the migration.

If you receive this error (You must run the sc:switch-to-database command before running this command.) when running the sc:migrate-to-database command, you should ensure your SC 'content config' looks like this:

1// config/simple-commerce.php
3'content' => [
4 'coupons' => [
5 'repository' => \DoubleThreeDigital\SimpleCommerce\Coupons\EntryCouponRepository::class,
6 'collection' => 'coupons',
7 ],
9 'customers' => [
10 'repository' => \DoubleThreeDigital\SimpleCommerce\Customers\EloquentCustomerRepository::class,
11 'model' => \DoubleThreeDigital\SimpleCommerce\Customers\CustomerModel::class,
12 ],
14 'orders' => [
15 'repository' => \DoubleThreeDigital\SimpleCommerce\Orders\EloquentOrderRepository::class,
16 'model' => \DoubleThreeDigital\SimpleCommerce\Orders\OrderModel::class,
17 ],
19 'products' => [
20 'repository' => \DoubleThreeDigital\SimpleCommerce\Products\EntryProductRepository::class,
21 'collection' => 'products',
22 ],

If you re-run the command, it should then run as expected.

Control Panel interface

When you make the switch, Simple Commerce will install Runway, another addon by me (Duncan McClean). Runway is the thing which lets you manage your database records/Eloquent models in the Control Panel.

Runway has it's own documentation site - you may read it if you please.

Custom Columns

There are cases where you may wish to add columns to either of the provided tables: orders/customers. You may do this by simply writing a migration to add a column to the table:

1php artisan make:migration AddPickupPointColumnToOrdersTable
2 * Run the migrations.
3 *
4 * @return void
5 */
6public function up()
8 Schema::table('orders', function (Blueprint $table) {
9 $table->string('pickup_point')->after('gateway');
10 });

You should then run your migrations:

1php artisan migrate

Once migrated, Simple Commerce will get/set any order or customer data to your custom column, rather than relying on it being saved to the data column.


If you need to, there's way to customise/override the Eloquent model used, along with the 'repository'.

The Model

First, in order to customise the Eloquent model, you'll need to create your own version of the Model in your app, then tell Simple Commerce to use that version instead of the default.

  1. Copy the OrderModel/CustomerModel class from inside Simple Commerce into your site's App\Models directory. You will need to also update the 'namespace' of the class.
  2. In your simple-commerce.php config file, replace the model reference with your own:
1'model' => \DoubleThreeDigital\SimpleCommerce\Orders\OrderModel::class,
2'model' => \App\Models\Order::class,

And there you go... that's you using a custom version of the Eloquent model.

The 'Repository'

Note: Customising the repository could lead to some bug fixes not being passed down into your app in the future.

  1. Create a repository class which extends the default one provided by Simple Commerce
  2. In your simple-commerce.php config file, with a reference to your new repository:
1'repository' => \DoubleThreeDigital\SimpleCommerce\Orders\EloquentOrderRepository::class, //
2'repository' => \App\SimpleCommerce\EloquentOrderRepository::class, //

And, in theory: that should be you!