Supabase

How to use Supabase with Laravel?

Discover how to seamlessly integrate Supabase with Laravel in this detailed guide. Follow the easy-to-understand steps to boost your web projects with efficiency.

Developer profile skeleton
a developer thinking

Overview

Connecting Supabase with Laravel involves using Supabase as your backend service for real-time databases and authentication while leveraging Laravel's powerful PHP framework. Begin by setting up a Supabase project and generating the necessary API keys. In Laravel, configure these credentials, usually in the .env file. Install relevant packages, like guzzlehttp/guzzle for HTTP requests or any specific Supabase SDK you plan to use. Custom scripts or service classes in Laravel can be crafted to interact with Supabase endpoints for data operations, authentication, and more. Understanding the core functionalities of both Laravel and Supabase will make implementation much smoother.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to use Supabase with Laravel?

Step 1: Set Up Laravel Project

  • First things first, grab Composer from getcomposer.org.
  • Then, let's create a new Laravel project:
    composer create-project --prefer-dist laravel/laravel your_project_name
    
  • Now, hop into the project directory:
    cd your_project_name
    

Step 2: Install Required Packages

  • You'll need the pg PHP extension since Supabase uses PostgreSQL. Install it based on your OS guidelines.
  • Also, grab the laravel/sanctum package for authentication, if you need it:
    composer require laravel/sanctum
    
  • Run migrations for Sanctum:
    php artisan migrate
    

Step 3: Set Up Supabase Account and Project

  • Head over to supabase.io and sign up or log in.
  • Create a new project and make sure to note down the database URL and the API key.

Step 4: Configure Environment Variables

  • Open up the .env file in your Laravel project.
  • Set the necessary database configuration using your Supabase credentials:
    DB_CONNECTION=pgsql
    DB_HOST=[your_supabase_host]
    DB_PORT=5432
    DB_DATABASE=[your_database_name]
    DB_USERNAME=[your_database_user]
    DB_PASSWORD=[your_database_password]
    

Step 5: Update Database Configuration

  • Open config/database.php and make sure the pgsql configuration is correct:
    'pgsql' => [
      'driver'   => 'pgsql',
      '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'   => '',
      'schema'   => 'public',
      'sslmode'  => 'prefer',
    ],
    

Step 6: Run Laravel Migrations

  • Once your environment is all set up, you can run your migrations to create the necessary tables:
    php artisan migrate
    

Step 7: Set Up Supabase Client

  • Install the Supabase PHP client or use Guzzle (HTTP client) to make API calls:
    composer require guzzlehttp/guzzle
    

Step 8: Configure Supabase in Laravel

  • Create a new service class or use a controller to interact with Supabase:
    ```
    use GuzzleHttp\Client;

class SupabaseService
{
protected $client;
protected $supabaseUrl;
protected $supabaseKey;

public function __construct()
{
    $this->client = new Client();
    $this->supabaseUrl = config('services.supabase.url');
    $this->supabaseKey = config('services.supabase.key');
}

public function fetchFromSupabase($endpoint)
{
    $response = $this->client->request('GET', "{$this->supabaseUrl}/rest/v1/{$endpoint}", [
        'headers' => [
            'apikey' => $this->supabaseKey,
            'Authorization' => "Bearer {$this->supabaseKey}"
        ]
    ]);

    return json_decode($response->getBody()->getContents(), true);
}

}

- Update the `config/services.php` to add Supabase configurations:

'supabase' => [
'url' => env('SUPABASE_URL'),
'key' => env('SUPABASE_KEY'),
],


<div style="color:#0065ff"><h3>Step 9: Use SupabaseService in Controller</h3></div>

- Inject the `SupabaseService` into your controller:

class UserController extends Controller
{
protected $supabaseService;

public function __construct(SupabaseService $supabaseService)
{
    $this->supabaseService = $supabaseService;
}

public function index()
{
    $data = $this->supabaseService->fetchFromSupabase('users');
    return view('users.index', compact('data'));
}

}


- Now, access `UserController@index` to fetch data from Supabase and render it in your view.

By following these steps, Supabase can be effectively used with a Laravel project to leverage its PostgreSQL database and other features.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.