Does Laravel Work With Firebase?
Laravel and Firebase work together seamlessly—use Firebase for auth and real-time data while Laravel handles your backend business logic and API layer.
Quick Facts
How Laravel Works With Firebase
Laravel integrates with Firebase primarily through the Firebase Admin SDK for PHP, allowing you to authenticate users, access Firestore/Realtime Database, and manage Firebase services directly from your Laravel application. The typical architecture involves Laravel serving as your API backend while Firebase handles authentication (via Firebase Auth) and optionally real-time data synchronization. You install the Firebase SDK via Composer, initialize it with your service account credentials, and interact with Firebase services through dependency injection or facades. This is ideal when you want Laravel's routing, middleware, and ORM capabilities combined with Firebase's managed infrastructure. The developer experience is clean—Firebase handles scaling and real-time features while Laravel remains your familiar request-response handler. One common pattern is using Laravel as a REST/GraphQL API that validates requests and delegates data operations to Firestore, with Firebase Auth tokens validated via Laravel middleware to secure your endpoints.
Best Use Cases
Quick Setup
composer require kreait/firebase-php<?php
namespace App\Services;
use Kreait\Firebase\Factory;
class FirebaseService
{
protected $database;
public function __construct()
{
$factory = (new Factory())
->withServiceAccount(config('firebase.credentials'));
$this->database = $factory->createDatabase();
}
public function getUser($uid)
{
return $this->database
->getReference('users/' . $uid)
->getValue();
}
public function saveUser($uid, $data)
{
$this->database
->getReference('users/' . $uid)
->set($data);
}
}
// In your Laravel controller:
app(FirebaseService::class)->saveUser('user123', ['name' => 'John']);Known Issues & Gotchas
Firebase Admin SDK requires a service account JSON file with sensitive credentials
Fix: Store the JSON file outside your web root, load via environment variables, and never commit to version control. Use Laravel's config system to manage the path securely.
Firestore queries lack some SQL capabilities like complex JOINs, making data modeling different from traditional Laravel/SQL patterns
Fix: Design your Firestore schema with denormalization in mind. Use Laravel's API endpoints to aggregate and join data server-side rather than expecting Firestore to do it.
Firebase Auth tokens expire quickly (1 hour) and refresh tokens have longer lifespans, requiring careful token management in your Laravel API
Fix: Implement middleware to validate Firebase ID tokens and refresh them as needed. Cache validation results to reduce repeated API calls.
Firestore's free tier has limits on read/write operations that scale poorly with typical Laravel request volumes
Fix: Cache responses using Laravel's cache drivers (Redis/Memcached) to reduce database hits and plan billing accordingly.
Alternatives
- •Laravel + AWS (DynamoDB/Cognito): More control but requires more setup; better for complex relational needs
- •Laravel + Supabase: Open-source Firebase alternative with PostgreSQL backend, easier if you prefer SQL
- •Next.js/Nuxt + Firebase: Full frontend-to-backend JavaScript stack without a separate API layer
Resources
Related Compatibility Guides
Explore more compatibility guides