Prerequisites
- PHP — This tutorial uses
v8.2
- Composer
- A PlanetScale account
Set up the Laravel app
This guide will integrate a simple Laravel 12 app with PlanetScale. The application displays a list of users from your PlanetScale database. The sample repo contains migrations and seed data to create and populate theusers
table. If you have an existing application, you can also use that.
1
Clone the starter Laravel application:
2
Enter into the folder and install the dependencies:You may need to run
composer update
if you haven’t updated in a while.3
Copy the
.env.example
file into .env
and generate the app key:Set up the database
Next, you need to set up your PlanetScale database and connect to it in the Laravel application.NoteIf you have an existing cloud-hosted database, you can choose the “Import” option to import your database to PlanetScale using our Import tool. If you go this route, we recommend using our Database Imports documentation.
- Name — You can use any name with lowercase, alphanumeric characters, or underscores. We also permit dashes, but don’t recommend them, as they may need to be escaped in some instances.
- Region — Choose the region closest to you or your application. It’s important to note if you intend to make this branch a production branch, you will not be able to change the region later, so choose the region with this in mind.
- Storage option — Choose a storage option. You can choose between network-attached storage or Metal for storage. For more information, see the plans documentation.
- Cluster size — Select the desired cluster size for your database.
main
, is automatically created when you create your database. Safe migrations are turned off by default, so you can make schema changes directly to this branch. Once you’re ready for production, you can turn on safe migrations to protect from accidental schema changes and enable zero-downtime deployments.
That’s it! Your database is ready to use. Next, let’s connect it to the Laravel application and then add some data.
Connect to the Laravel app
There are two ways to connect to PlanetScale:- With an auto-generated username and password
- Using the PlanetScale proxy with the CLI
Option 1: Connect with username and password (Recommended)
First, you need to generate a database username and password so that you can use it to connect to your application. You’ll be presented with this option after creating your database. You can also access the password creation page by clicking “Connect” -> “Create password”. As long as you’re an organization administrator, this will generate a username and password that has administrator privileges to the database.TipIf the password value is blurred, you need to click “New password” to generate a new one.
.env
tab and paste them into your own .env
file in your Laravel application. The structure will look like this:
DB_DATABASE
, you can use your PlanetScale database name directly if you have a single unsharded keyspace. If you have a sharded keyspace, you’ll need to use @primary
. This will automatically direct incoming queries to the correct keyspace/shard. For more information, see the Targeting the correct keyspace documentation.
The MYSQL_ATTR_SSL_CA
value is platform-dependent. Please refer to our documentation around how to connect to PlanetScale securely for the platform you’re using.
Option 2: Connect with the PlanetScale proxy
To connect with the PlanetScale proxy, you need to install and use the PlanetScale CLI.1
Open a connection by running the following:If you’re following this guide exactly and haven’t created any branches, you can use the default branch,
main
.2
A secure connection to your database will be established and you’ll see a local address you can use to connect to your application.
3
Open the The connection uses port
.env
file in your Laravel app and update it as follows:3306
by default, but if that’s being used, it will pick a random port. Make sure you paste in whatever port is returned in the terminal. You can leave DB_USERNAME
and DB_PASSWORD
blank.Run migrations and seeder
Now that you’re connected, let’s add some data to see it in action. The sample application comes with some default Laravel migration files,database/migrations/
, to create the database schema. It also contains a user seeder to seed some mock user data.
NoteLaravel uses foreign key constraints by default. PlanetScale, however, has foreign key constraint support turned off by default. For this tutorial, we’re keeping the Laravel defaults, so you need to enable foreign key constraint support in your database settings page. Click the checkbox next to “Allow foreign key constraints” and press “Save database settings”.
1
In the root of the Laravel project, run the following to migrate and seed the database:
2
Start the application:
- Refresh your Laravel homepage and you’ll see a list of users.

Add data manually
If you want to continue to play around with adding data on the fly, you have a few options:- PlanetScale dashboard console
- Laravel Tinker
- PlanetScale CLI shell
- Your favorite MySQL client (for a list of tested MySQL clients, review our article on how to connect MySQL GUI applications)
Add data in PlanetScale dashboard console
PlanetScale has a built-in console where you can run MySQL commands against your branches. By default, web console access to production branches is disabled to prevent accidental deletion. From your database’s dashboard page, click on the “Settings” tab, check the box labelled “Allow web console access to production branches”, and click “Save database settings”. To access it, click “Console” > select your branch > “Connect”. From here, you can run MySQL queries and DDL against your database branch.1
Add a record to the
users
table:2
Refresh the Laravel homepage to see the new record. You can also verify it was added in the console with:

What’s next?
Once you’re done with initial development, you can enable safe migrations to protect from accidental schema changes and enable zero-downtime deployments. To learn more about PlanetScale, take a look at the following resources:- PlanetScale workflow — Quick overview of the PlanetScale workflow: branching, non-blocking schema changes, deploy requests, and reverting a schema change.
- PlanetScale branching — Learn how to utilize branching to ship schema changes with no locking or downtime.
- PlanetScale CLI — Power up your workflow with the PlanetScale CLI. Every single action you just performed in this quickstart (and much more) can also be done with the CLI.