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.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Connect Laravel with Firebase Real Time Database

Updated on  31st March

5 Min Read
firebaseone
Reading Time: 5 minutes

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).

Prerequisites

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.

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:

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

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:

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:

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

 Firebase Database

Conclusion

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

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 pardeep.kumar@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

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

THERE’S MORE TO READ.