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.
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.
composer create-project --prefer-dist laravel/laravel your_project_name
cd your_project_name
pg
PHP extension since Supabase uses PostgreSQL. Install it based on your OS guidelines.laravel/sanctum
package for authentication, if you need it:composer require laravel/sanctum
php artisan migrate
.env
file in your Laravel project.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]
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',
],
php artisan migrate
composer require guzzlehttp/guzzle
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 our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.
Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.
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.
With the Bootstrapped platform, managing projects and developers has never been easier.
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.
Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.
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.
Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.
Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.
Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.
Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.