This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

✨ Vultr and Linode are back on Cloudways! VIEW PLANS→

Connect Laravel with Firebase Real Time Database

Updated on December 22, 2021

4 Min Read

Secure and efficient data retrieval is one of the fundamental requirements for a good app. Fortunately, developers have a number of options (for instance, framework level tools and secure DBMS) for implementing this requirement.

What is Firebase?

Firebase was founded in 2012 as a real-time architecture for syncing data between apps. It was acquired by Google in 2014. Originally, it was a part of Envolve, a real-time chat integration API. Overtime, the developers realized that the system was being used to more than just text messages. Realizing the potential of the system, they come up with the idea of Firebase as a separate entity from Envolve.

In this article , I will demonstrates how to connect Firebase Realtime Database in Laravel 5.5 (by using Admin SDK).


For the purpose of this tutorial, I assume that you have a Laravel application installed on a web server. My setup is:

  • Laravel 5.5
  • PHP 7.1
  • Google Firebase Realtime Database
  • Web server (Apache, NGINX or integrated PHP web server for testing).

I have installed my Laravel app on a Cloudways managed server because it has a Laravel optimized dev stack and advanced PHP hosting. If you do not have an account on Cloudways, sign up for free, and check out the following GIF to setup the server and application in just a few clicks.


You might also like: Enhanced Cloudways Staging Environment Is Now Available for All Users

How to Setup Firebase

Let’s start with signing up for a Gmail account. In the next tab, open the Firebase page for database settings and click the Visit Console button.

Next, in the next window, create a database project and provide information including project name. When done, click the Create Project button.

Wait for the project to be created and then continue. You will be automatically redirected to the Firebase Dashboard.

Lightning Fast Laravel Hosting With 24×7 Tech Support

We Take Care of Your Hosting Hassles To Let You Focus More On The Business

After successfully creating the project, go to the Database tab, click Rules to update read and write rules to true and then click the Publish button.

Generate the API Key

Firebase generates an  API key for your project that you can download and paste into http/Controller.

Click the User and permissions tab.

Next, click Support account and then click the Create Key button

The key will be created in JSON format and downloaded automatically. You can copy the this file to the http/controller folder of Laravel.

Laravel Composer Require

After setting up the Firebase database, go to the Laravel application and install kreait/firebase-php ^4.0. Open the SSH terminal and goto the public_root

Enter the following command:

composer require kreait/firebase-php ^4.0

Alternatively, you can specify the Firebase Admin SDK as a dependency in your project’s existing composer.json file:


"require": {

"kreait/firebase-php": "^4.0"



laravel banner cta

Create the Controller

After pasting the API Key in the Controller folder, go to the in SSH Terminal and type the following command to create the Controller:

php artisan make:controller FirebaseController

Next, paste the following code in the Controller file:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Kreait\Firebase;

use Kreait\Firebase\Factory;

use Kreait\Firebase\ServiceAccount;

use Kreait\Firebase\Database;

class FirebaseController extends Controller



public function index(){

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/Laraveltesting-6aeda3a963f2.json');

$firebase = (new Factory)




$database = $firebase->getDatabase();

$newPost = $database



'title' => 'Post title',

'body' => 'This should probably be longer.'


//$newPost->getKey(); // => -KVr5eu8gcTv7_AHb-3-

//$newPost->getUri(); // =>

//$newPost->getChild('title')->set('Changed post title');

//$newPost->getValue(); // Fetches the data from the realtime database







Remember to replace the file path in fromJsonFile with your own file path. Also, replace the database URL with your database’s URL:

Set up the Route

Open  the Laravel routes/web.php file and update the following code:




| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('/', function () {

return view('welcome');



Save all changes in the Laravel application and run your application to see the output in action:

 Firebase Database


I hope that by now you have a very good idea of using Firebase Realtime Database in Laravel project. To see my sample app in action, just check out the demo. If you need help, just drop a comment and I will get back to you


Share your opinion in the comment section. COMMENT NOW

Share This Article

Pardeep Kumar

Pardeep is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He love to work on Open source platform , Frameworks and working on new ideas. You can email him at [email protected]


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now