In this get-started guide, we'll show you how to get up and running with Laravel and SingleStoreDB in a matter of minutes. You can view the full example repository at github.com/singlestore-labs/start-with-laravel, or follow along here.
If your machine is not already set up with Composer, we recommend reading their getting started documentation. You may also consult Laravel's getting started documentation.
To get started, create a fresh Laravel project.
# Create a new Laravel project.
composer create-project laravel/laravel singlestoredb-example
cd singlestoredb-example
Then require the SingleStoreDB database driver for Laravel.
# Require the SingleStoreDB driver for Laravel.
composer require singlestoredb/singlestoredb-laravel
Now we need to update your config/database.php to use this new SingleStoreDB driver. Here, we're going to create a new connection named singlestore in the connections key of the configuration. Notice that it's almost an exact copy of the MySQL configuration, but with the driver set to singlestore.
'singlestore' => [
'driver' => 'singlestore',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'unix_socket' => env('DB_SOCKET'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_PERSISTENT => true
]) : [],
],
Over in your .env file, change the DB_CONNECTION to point to the new singlestore connection. You'll also need to set the rest of the DB_* variables to point to your SingleStoreDB instance.
DB_CONNECTION=singlestore
DB_HOST= # SingleStoreDB URL
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
There are a few of the default Laravel migrations that you'll need to modify before you can run php artisan:migrate. Mainly, we are changing unique constraints to plain indexes, to be compatible with SingleStoreDB's sharding strategy. If you need to enforce uniqueness, you can either change these migrations further or enforce uniqueness at the application layer.
First, publish the Sanctum migrations so that we're able to modify them.
php artisan vendor:publish --tag=sanctum-migrations
Then make the following changes:
- Open 2014_10_12_000000_create_users_table.php and change the unique key on the email column to index.
- Open 2019_08_19_000000_create_failed_jobs_table.php and change the unique key on the uuid column to index.
- Open 2019_12_14_000001_create_personal_access_tokens_table.php and change the unique key on the token column to index.
That's it! You can now run php artisan migrate and you should see all of your tables in SingleStoreDB.
You're fully up and running with Laravel and SingleStoreDB.
For further reading, you can check out the SingleStoreDB documentation, or the SingleStoreDB Laravel driver documentation.