How to connect supabase with laravel?

Learn how to integrate Supabase with Laravel to leverage the power of an open-source Firebase alternative. This guide provides step-by-step instructions on setting up a secure connection between your Laravel application and Supabase.

Step 1 : Setup Supabase Account

In order to set up a database and storage, you'll require a Supabase account. You can create one by visiting this URL : https://supabase.com/dashboard/sign-in

Once you've successfully logged in, your next step is to establish a project or database, which should appear somewhat as follows.


Note : Ensure that you securely store your database password in a safe location.


Once the project is created, you'll be redirected to the project dashboard. Here, you need to click on the 'Connect' button, as shown in the image below.


After you click the 'Connect' button, a popup will appear. In the first tab, select 'Connection String', and in the subsequent tab, choose 'URI', as depicted in the image below.


Securely store the copied URI. Then, navigate to the project settings, which can be found in the sidebar. From there, copy the Project URL and the Anonymous Public Key (anon public), and store them in a safe place.


Note : Project URL and the Anonymous Public Key (anon public) will be used for storage, If you are not using supabase storage you can skip it.


We've collected all the necessary information to link our Supabase project with Laravel. Let's proceed with establishing the Laravel connection.

Step 2 : Connect With Laravel

It's presumed that your Laravel project is already up and running. Therefore, please open the .env file. We need to update the database connection details there. Start by deleting the existing database connection and then input the Supabase credentials, Something like this :


DB_CONNECTION="pgsql"
DATABASE_URL="postgres://postgres.ljwg****:[YOUR-PASSWORD]@aws-0-ap-south-1.pooler.supabase.com:6543/postgres"

If you are going to use Supabase storage, make sure to include the anonymous public key (anon public key) and the project URL as well, Something like this : 


SUPABASE_ANON_KEY="eyJhbGciOiJIUzI1NiIsInR5c*****"
SUPABASE_PROJECT_URL="https://qtnszmzp****.supabase.co"

Next, we need to confirm that the psql extension is activated on your server. To do this, open the php.ini file and verify if the following two are enabled.

extension=pdo_pgsql
extension=pgsql

It's highly likely that this extension is already present in your php.ini file, but it might be commented out with a semicolon (;) at the beginning. Simply remove the semicolon to enable it. If the extension doesn't exist, you can add it after `extension=pdo_mysql`.

Now lets verify if pgsql extension is activated, To do that you can create a basic route function in web.php 

Route::get('phpinfo', function () {
    return phpinfo();
});


If it's activated, we can then verify if Laravel is successfully connected with Supabase. To do that, let's add a new route function in the web.php.

Route::get('test-connection', function ()
{
    try
    {
        $pdo = \Illuminate\Support\Facades\DB::connection()->getPDO();
        $dbname = \Illuminate\Support\Facades\DB::connection()->getDatabaseName();
        dd($dbname);
    }
    catch (\Exception $e)
    {
        dd($e->getMessage());
    }
});

If the database name isn't 'postgres', there may have been an issue. Otherwise, everything is in order and you can migrate your database to Supabase just as you would with MySQL using artisan command, with no differences.

Tags

Subscribe Our Newsletters