Skip to content

Updated

Use Supabase with Laravel

Learn how to create a PHP Laravel project, connect it to your Supabase Postgres database, and configure user authentication.

Agent Prompt

Help me add Supabase to my Laravel project. Create a Supabase project at database.new. Then:

  1. Run composer create-project laravel/laravel example-app to scaffold the project.
  2. Install Laravel Breeze with composer require laravel/breeze --dev && php artisan breeze:install.
  3. Copy the Session Pooler connection string from the Supabase Connect panel and set DB_URL in .env. Also set DB_SSLMODE=require.
  4. Set search_path to a custom schema (e.g. laravel) in config/database.php.
  5. Run php artisan migrate to apply database migrations.
  6. Run php artisan serve and open http://127.0.0.1:8000.

REFERENCE https://supabase.com/docs/guides/getting-started/quickstarts/laravel.md

1. Create a Supabase project

To start, you need a Supabase project.

Create a new Supabase project from the Dashboard of any organization you belong to.

Save your database password securely. You need it for the connection string.

2. Create a Laravel project

Make sure your PHP and Composer versions are up to date, then use composer create-project to scaffold a Laravel project.

For more information, see Creating a Laravel project in the Laravel documentation.

composer create-project laravel/laravel example-app

3. Set up AI tooling (optional)

Supabase provides two ways to give AI tools context about your project: Agent Skills, which give your AI coding agent procedural knowledge, and the MCP server, which connects AI assistants to your Supabase project directly.

Agent Skills

Agent Skills is a curated set of instructions that give your AI agent procedural knowledge about working with Supabase.

Install them so your AI coding agent can produce more accurate, reliable code using current Supabase patterns, such as authentication, server-side rendering, and database migrations, rather than relying solely on training data.

Installing Agent Skills

To install, run the following command in the root of your project:

npx skills add supabase/agent-skills

Supabase MCP server

The Supabase MCP server connects AI assistants to Supabase, so they can inspect your schema and act on your projects on your behalf. Find out how to add it to your client in the MCP docs.

4. Install the authentication template

Install Laravel Breeze, a basic implementation of all of Laravel's authentication features. It ships the migrations that you run in Run the database migrations.

composer require laravel/breeze --dev
php artisan breeze:install blade

When prompted, pass a stack name such as blade, react, or vue. The preceding command uses Blade templates.

5. Set up the Postgres connection details

  1. Navigate to your project dashboard and click on Connect.

  2. Look for the Session pooler connection string and copy it. Replace the password placeholder with your saved database password, and percent-encode any reserved characters it contains, such as &, #, ?, or a space. If you don't have your database password, you can reset it in your Database Settings.

  3. Set sslmode=require either on the connection string itself or as an explicit config option if your framework sets it separately. Most drivers default to prefer, which falls back to sending your data in plaintext if the encrypted attempt fails. You can also enforce SSL on the database side.

The connection strings below show the format only. Take the host, port, and username from the string you copied rather than typing the bracketed placeholders literally.

.env
DB_CONNECTION=pgsql
DB_URL=postgres://postgres.[PROJECT-REF]:[YOUR-DATABASE-PASSWORD]@[POOLER-HOST]:5432/postgres
DB_SSLMODE=require

The generated config/database.php reads sslmode from DB_SSLMODE, which defaults to prefer.

6. Change the default schema

By default, Laravel uses the public schema. We recommend changing the schema, because Supabase exposes the public schema as a data API.

To change the schema, set the search_path variable in config/database.php.

The schema you specify in search_path must exist on Supabase. You can create a schema from the Table Editor.

config/database.php
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DB_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8'),
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'laravel',
'sslmode' => env('DB_SSLMODE', 'prefer'),
],

7. Run the database migrations

Laravel ships with database migration files that set up the required tables for Laravel Authentication and User Management.

php artisan migrate

8. Start the app

Run the development server. Go to http://127.0.0.1:8000 in a browser to see your app. You can also go to http://127.0.0.1:8000/register and http://127.0.0.1:8000/login to register and sign in users.

php artisan serve

Production requirements

The quickstart procedure in this guide optimizes for getting you to a working app, not for production.

Before you deploy:

  • If your app reads or writes through the Data API, review your Row Level Security policies. Any policy you added here is scoped to this quickstart's sample data, not to real user data.
  • Set your Supabase credentials as environment variables on whatever platform you deploy to, rather than committing them to source control.
  • Configure a custom domain for your Supabase project once you're ready to go live.

Next steps

Build on this app with the following resources: