To integrate Laravel with Supabase, you need to follow these steps:
1. Create a Supabase Project:
- Go to the Supabase website and create a new project. This will give you a Postgres database and other features like authentication and APIs[1][4].
2. Set Up Laravel Project:
- Create a new Laravel project using Composer:
bash
composer create-project laravel/laravel example-app
- Install Laravel Breeze for authentication:
bash
composer require laravel/breeze --dev
php artisan breeze:install
- Configure the Postgres database connection in `config/database.php`:
php
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'laravel',
'sslmode' => 'prefer',
],
- Update the `DB_CONNECTION` environment variable to `pgsql` in your `.env` file:
ini
DB_CONNECTION=pgsql
DATABASE_URL=postgres://postgres:your_password@your_host:5432/your_database
- Run the database migrations to set up the tables:
bash
php artisan migrate
- Start the Laravel server:
bash
php artisan serve
3. Connect to Supabase Database:
- Go to the Supabase dashboard and navigate to the database settings. Copy the URI connection string.
- Update the `DATABASE_URL` environment variable in your `.env` file with the Supabase URI:
ini
DATABASE_URL=postgres://your_username:your_password@your_host:5432/your_database
- Update the `search_path` variable in `config/database.php` to match your Supabase schema:
php
'search_path' => 'your_schema',
4. Verify Setup:
- Run the migrations again to ensure the tables are set up correctly:
bash
php artisan migrate
- Start the Laravel server and verify that your application is working:
bash
php artisan serve
5. Optional: Use Supabase Auth:
- If you want to use Supabase's authentication features, you need to configure Laravel to use Supabase Auth. This involves setting up the Supabase API keys and configuring Laravel to use them for authentication[1][4].
By following these steps, you can integrate your Laravel application with Supabase and use its features like authentication and Postgres database management.
Citations:[1] https://supabase.com/blog/laravel-postgres
[2] https://www.youtube.com/watch?v=dg1suKt-7D4
[3] https://github.com/rafaelwendel/phpsupabase
[4] https://ahmadrosid.com/blog/how-to-use-supabase-with-laravel
[5] https://www.reddit.com/r/Supabase/comments/1cl7agg/integration_with_laravel/