To use Laravel with Firebase, use the “kreait/laravel-firebase” package.
Step 1: Install Laravel Project
composer create-project --prefer-dist laravel/laravel laravelfirebase
Step 2: Install laravel-firebase package
Open your terminal and enter the following command.
composer require kreait/laravel-firebase
If, for some reason, you experience an error during installation regarding the platform requirements, you can add the –ignore-platform-reqs flag that will install the package ignoring the underlying platform requirements.
composer require kreait/laravel-firebase --ignore-platform-reqs
Step 3: Setup Firebase
Let’s begin with signing up for a Gmail account. But first, Open Google Firebase Console by clicking on this link Google Firebase Console. Looks like the below image.
Next window, build a database project and provide information, including the project name. When finished, click the Create Project button.
Wait for the project to be created, and then continue. You will be automatically switched to the Firebase Dashboard.
After successfully building the project, move to the Database tab, tick Rules to update, read and write rules to true, and press the Publish button.
Step 4: Generate API Key
Firebase makes an API key for your project. Go to Project settings >> SERVICE ACCOUNTS.
We then need to download the JSON file containing the project’s credentials by clicking the Generate new private key.
Once the file is downloaded, we can rename the JSON configuration file and add it to the base of our Laravel application.
We first need to publish the default Firebase configurations with the package to use our project credentials. We use the following command:
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config
This command creates a config/firebase.php file that contains all the Firebase configurations needed to connect to our Firebase project.
We then need to specify the environmental variables in the .env file.
We can now add our project configurations to our Laravel .env file.
FIREBASE_CREDENTIALS=/full/path/to/firebase_credentials.json
# or
FIREBASE_CREDENTIALS=relative/path/to/firebase_credentials.json
We can create an authentication here, but for this tutorial, we will not do it because our main focus is to create a CRUD application.
Step 5: Create a Controller
php artisan make:controller FirebaseController --resource
It will create one controller file called FirebaseController.php.
Add the following code to the FirebaseController.php file.
//FirebaseController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Kreait\Firebase; use Kreait\Firebase\Factory; use Kreait\Firebase\ServiceAccount; class FirebaseController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/laravelfirebase-9d875-firebase-adminsdk-wltre-a1b8486a6c.json'); $firebase = (new Factory) ->withServiceAccount($serviceAccount) ->withDatabaseUri('https://laravelfirebase-9d875.firebaseio.com/') ->create(); $database = $firebase->getDatabase(); $newPost = $database ->getReference('blog/posts') ->push([ 'title' => 'Laravel FireBase Tutorial' , 'category' => 'Laravel' ]); echo '<pre>'; print_r($newPost->getvalue()); } }
Step 6: Add Route
The next step is to define the route in routes >> web.php file.
Route::get('firebase','FirebaseController@index');
Start Development Server by using the following command in the terminal.
php artisan serve
Go to this URL: http://localhost:8000/firebase.
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
please can u give the structure of your config file
I have follow instruction but i have an error
“syntax error, unexpected ‘$serviceAccount’ (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)”
in line :
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.’/laravelfirebase-9d875-firebase-adminsdk-wltre-a1b8486a6c.json’);
Thanks alot bro you made my day
hi i follow your step but i am facing an error cURL error 60: SSL certificate problem: unable to get local issuer certificate. i find the solutions it to set its path in php.ini i had try this but it was not working for me
hi i follow your step but i am facing an error cURL error 60: SSL certificate problem: unable to get local issuer certificate. i find the solutions it to set its path in php.ini i had try this but it was not working for me
hi i follow your step but i am facing an error cURL error 60: SSL certificate problem: unable to get local issuer certificate. i find the solutions it to set its path in php.ini i had try this but it was not working for me
Hey can you implement the complete crud functionality
thanks my bro
hi bro , thanks for this tutorial . can you pls make tutorial for cloud firestore and laravel
how to update row??
THANKS!!!!
I followed the tutorial but didn’t work for me, I’m using laravel 5.8
Call to private method Kreait\Firebase\ServiceAccount::fromJsonFile() from context
I face this issue after implementation