Yes, Laravel can use Supabase as its database and interact with it. To connect Laravel with Supabase, you can follow these steps:
- Install the Supabase PHP package using Composer:
composer require supabase/supabase-php
- In your Laravel project, open the
.envfile and add the Supabase credentials:
SUPABASE_URL=https://your-supabase-url.supabase.co
SUPABASE_KEY=your-supabase-key
- Create a new database connection in the
config/database.phpfile:
'supabase' => [
'driver' => 'pgsql',
'url' => env('SUPABASE_URL'),
'host' => parse_url(env('SUPABASE_URL'), PHP_URL_HOST),
'port' => parse_url(env('SUPABASE_URL'), PHP_URL_PORT),
'database' => parse_url(env('SUPABASE_URL'), PHP_URL_PATH),
'username' => null,
'password' => env('SUPABASE_KEY'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
- Update the default database connection in the same
config/database.phpfile to use the Supabase connection:
'default' => env('DB_CONNECTION', 'supabase'),
- Now you can use the Supabase connection in your Laravel application. For example, to retrieve all records from a table named
users, you can create a new controller method like this:
use Illuminate\Support\Facades\DB;
public function getUsers()
{
$users = DB::connection('supabase')->select('select * from users');
return $users;
}
Make sure to replace users with the actual table name in your Supabase database.
That's it! You should now be able to connect Laravel with Supabase and interact with the database.