How To Use Firebase Realtime Database With Laravel Without Any Package Step By Step Guide

image
image
image
image
image
image
image
image
How to Use Firebase Realtime Database with Laravel Without Any Package (Step-by-Step Guide)

How to Use Firebase Realtime Database with Laravel Without Any Package (Step-by-Step Guide)

Before Jumping towards the below integration you need the firebase database url

For that part log in to your firebase via this link - https://firebase.google.com/

Then you have to create a project. after creating a project you have the option like the below one, choose firebase real Realtime Database as shown in the below image.

Then click on the project overview -> project settings

Then generate the key and download the file

Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data in real time across all clients. While many tutorials rely on Firebase PHP packages like kreait/firebase-php, sometimes you want full control — or just prefer not to use any extra packages.

This blog will walk you through how to integrate Firebase Realtime Database in your Laravel application using just Laravel’s HTTP client and Firebase REST API, without installing any third-party packages.

Let’s get started.

Why Use Firebase Without a Package?

While Firebase PHP SDKs are convenient, sometimes:

  1. You want lightweight integration.
  2. You don’t want to install heavy dependencies.
  3. You need just basic CRUD operations.
  4. You want to learn how the underlying Firebase REST API works.

This approach gives you full control over how data is sent and received, and is useful for small or custom Firebase use cases.

Step 1: Set Up Firebase

First, create your Firebase project:

  1. Go to https://console.firebase.google.com
  2. Click “Add Project”
  3. Set a project name and enable/disable Google Analytics (optional)
  4. After creation, go to Build → Realtime Database
  5. Click “Create Database”
  6. Choose a location and start in test mode (only for development)

In test mode, anyone can read and write. Change this in security rules for production use.

Once created, you’ll get a Firebase Database URL, something like:

https://your-project-id.firebaseio.com/

Save this — we’ll need it in the Laravel .env file.

Step 2: Configure Laravel Project

Step 2.1: Create a New Laravel App (if needed)

If you're starting from scratch:

composer create-project laravel/laravel firebase-laravel
cd firebase-laravel

Step 2.2: Add Firebase URL to .env

Open your .env file and add:

FIREBASE_DATABASE_URL=https://your-project-id.firebaseio.com/

Update config/services.php if you want a central config:

'firebase' => [
'url' => env('FIREBASE_DB_URL'),
],

Step 3: Create a Controller for Firebase Operations

Run the artisan command:

php artisan make:controller FirebaseController

Open app/Http/Controllers/FirebaseController.php and update it:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class FirebaseController extends Controller
{
protected $baseUrl;

public function __construct()
{
$this->baseUrl = rtrim(env('FIREBASE_DB_URL'), '/') . '/';
}

public function store()
{
$response = Http::post($this->baseUrl . 'users.json', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);

return response()->json([
'firebase_response' => $response->json()
]);
}

public function fetch()
{
$response = Http::get($this->baseUrl . 'users.json');

return response()->json($response->json());
}

public function update($id)
{
$response = Http::patch($this->baseUrl . "users/{$id}.json", [
'name' => 'Updated Name'
]);

return response()->json($response->json());
}

public function delete($id)
{
$response = Http::delete($this->baseUrl . "users/{$id}.json");

return response()->json(['deleted' => $id]);
}
}

Step 4: Add Routes to web.php

use App\Http\Controllers\FirebaseController;

Route::get('/firebase/store', [FirebaseController::class, 'store']);
Route::get('/firebase/fetch', [FirebaseController::class, 'fetch']);
Route::get('/firebase/update/{id}', [FirebaseController::class, 'update']);
Route::get('/firebase/delete/{id}', [FirebaseController::class, 'delete']);

Step 5: Test Your API

Start your Laravel server:

php artisan serve

Now, try the following endpoints in your browser or Postman:

URLAction
/firebase/storeStores new user in Firebase
/firebase/fetchRetrieves all user records
/firebase/update/{id}Updates the name of a user
/firebase/delete/{id}Deletes a user

The {id} should be the Firebase record key you get from store or fetch.

Firebase Structure Example

When you hit /firebase/store, it sends:

{
"name": "John Doe",
"email": "john@example.com"
}

Firebase auto-generates a unique ID:

{
"name": "-NkqT3xkFBoP2ZUzEFX"
}

Then, your data appears like:

users: {
-NkqT3xkFBoP2ZUzEFX: {
name: "John Doe",
email: "john@example.com"
}
}

To update or delete, pass that key (-NkqT3xkFBoP2ZUzEFX) to the controller route.

Securing Your Firebase Database

After development, do not leave test mode enabled. Go to:

Firebase Console → Database → Rules, and set:

{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}

Then, you’ll need to add an auth token when accessing the REST API:

Http::get("https://your-project.firebaseio.com/users.json?auth=FIREBASE_ID_TOKEN");

When the data will be stored, it will be shown in the below manner on firebase


You can generate a token using Firebase Authentication or admin SDK.

Benefits of This Approach

  1. No extra package dependencies
  2. Lightweight and fast
  3. Simple REST API design
  4. Great for server-to-server communication
  5. Easily extendable for more complex workflows

When Should You Use This?

This approach is best for:

  1. Internal dashboards
  2. Admin scripts
  3. Small web/mobile integrations
  4. Learning how Firebase works under the hood

If you're building a full-featured Firebase app, then use a proper Firebase SDK for advanced features like authentication, cloud messaging, file uploads, etc.

Bonus Tips

  1. Use Laravel Queues to sync Firebase in the background
  2. Add caching if reading frequently
  3. Use Form Requests to validate input before sending to Firebase
  4. Use a service class to abstract Firebase logic from controllers

Summary

You've now learned how to:

  1. Set up a Firebase project
  2. Use Laravel’s native HTTP client to interact with Firebase’s REST API
  3. Store, fetch, update, and delete data — all without any package
  4. Secure your Firebase database
  5. Extend your Laravel app to talk directly to Firebase

This approach is clean, flexible, and powerful for use cases where you want to avoid SDK bloat.

Resources

  1. Firebase REST API Docs: https://firebase.google.com/docs/reference/rest/database
  2. Laravel HTTP Client: https://laravel.com/docs/http-client
  3. Firebase Security Rules: https://firebase.google.com/docs/database/security

Would you like a follow-up blog on using Firebase Authentication, Firestore, or Cloud Messaging with Laravel? Just let me know — I’d be happy to help!

Download The Project Directly From Github

https://github.com/codehunger-team/firebase-real-time-database-with-laravel