Use Supabase with Laravel
Learn how to create a PHP Laravel project, connect it to your Supabase Postgres database, and configure user authentication.
1. Create a Supabase project#
Before you can use Supabase, you need a Supabase project. You can create a project visually in the Dashboard or programmatically using the Management API.
Create a new Supabase project from the Dashboard of any organization you belong to.
You can also use database.new to create a new Supabase project.
2. Install agent skills (optional)#
Agent Skills are curated instruction sets that give your AI agent procedural knowledge about working with Supabase.
Install them in your project with:
1npx skills add supabase/agent-skills3. Set up your database#
When your Supabase project is up and running, create an instruments table with some sample data. Then set only the privileges each Postgres role needs, add Row Level Security (RLS) for enhanced security for database data by default, and create an RLS policy to make the data in the table publicly readable.
Save some steps by clicking here to prefill the SQL in the SQL Editor, and then clicking Run.
Or do this manually in your project's SQL Editor, by pasting the SQL below, and clicking Run.
1-- Create the table2create table instruments (3 id bigint primary key generated always as identity,4 name text not null5);67-- Insert sample data into the table8insert into instruments (name)9values10 ('violin'),11 ('viola'),12 ('cello');1314-- Grant the privileges the role needs, which is read access15grant select on public.instruments to anon;1617-- Enable row level security for the table18alter table instruments enable row level security;1920-- Create a policy to allow the anon role to read from the instruments table21create policy "public can read instruments"22on public.instruments23for select to anon24using (true);If you disabled the Data API during project setup, enable it in the Integrations > Data API section of the Dashboard and expose the specific tables or functions you want to access. To automatically grant access for new tables and functions in public, enable Automatically expose new tables.
4. Create a Laravel project#
Make sure your PHP and Composer versions are up to date, then use composer create-project to scaffold a new Laravel project.
See the Laravel docs for more details.
1composer create-project laravel/laravel example-app5. Install the authentication template#
Install Laravel Breeze, a basic implementation of all of Laravel's authentication features.
1composer require laravel/breeze --dev2php artisan breeze:install6. Set up the Postgres connection details#
Go to database.new and create a new Supabase project. Save your database password securely.
When your project is up and running, navigate to your project dashboard and click on Connect.
Look for the Session Pooler connection string and copy the string. You will need to replace the Password with your saved database password. You can reset your database password in your Database Settings if you do not have it.
If you're in an IPv6 environment or have the IPv4 Add-On, you can use the direct connection string instead of Supavisor in Session mode.
.env
1DB_CONNECTION=pgsql2DB_URL=postgres://postgres.xxxx:password@xxxx.pooler.supabase.com:5432/postgres7. Change the default schema#
By default Laravel uses the public schema. We recommend changing this as Supabase exposes the public schema as a data API.
You can change the schema of your Laravel application by modifying the search_path variable app/config/database.php.
The schema you specify in search_path has to exist on Supabase. You can create a new schema from the Table Editor.
app/config/database.php
1'pgsql' => [2 'driver' => 'pgsql',3 'url' => env('DB_URL'),4 'host' => env('DB_HOST', '127.0.0.1'),5 'port' => env('DB_PORT', '5432'),6 'database' => env('DB_DATABASE', 'laravel'),7 'username' => env('DB_USERNAME', 'root'),8 'password' => env('DB_PASSWORD', ''),9 'charset' => env('DB_CHARSET', 'utf8'),10 'prefix' => '',11 'prefix_indexes' => true,12 'search_path' => 'laravel',13 'sslmode' => 'prefer',14],8. Run the database migrations#
Laravel ships with database migration files that set up the required tables for Laravel Authentication and User Management.
Note: Laravel does not use Supabase Auth but rather implements its own authentication system!
1php artisan migrate9. Start the app#
Run the development server. Go to http://127.0.0.1:8000 in a browser to see your application. You can also navigate to http://127.0.0.1:8000/register and http://127.0.0.1:8000/login to register and log in users.
1php artisan serve