Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mattacosta_3's avatar

Supabase connection with Laravel

In trying to connect to a online database on my Supabases dashboard. I keep getting an error code that begins with: SQLSTATE[08006] [7] could not connect to server: Connection timed out (0x0000274C/10060) and then it asks if its running on the right host and if that port is accepting TCP/IP connections.

Can laravel use Supabase as its database and interact with it?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, Laravel can use Supabase as its database and interact with it. To connect Laravel with Supabase, you can follow these steps:

  1. Install the Supabase PHP package using Composer:
composer require supabase/supabase-php
  1. In your Laravel project, open the .env file and add the Supabase credentials:
SUPABASE_URL=https://your-supabase-url.supabase.co
SUPABASE_KEY=your-supabase-key
  1. Create a new database connection in the config/database.php file:
'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',
],
  1. Update the default database connection in the same config/database.php file to use the Supabase connection:
'default' => env('DB_CONNECTION', 'supabase'),
  1. 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.

Please or to participate in this conversation.