How to Integrate Firebase into Laravel

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.

Laravel Firebase Example

Next window, build a database project and provide information, including the project name. When finished, click the Create Project button.

Firebase Create Project

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

Firebase Login

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.

Firebase Database Read Write Access

 Step 4: Generate API Key

How to generate Firebase API key

Firebase makes an  API key for your project. Go to Project settings >>  SERVICE ACCOUNTS.

Laravel 5.6

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.

Array of data

Laravel 5.6 Firebase Example Tutorial

That’s it.

13 thoughts on “How to Integrate Firebase into Laravel”

  1. 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’);

    Reply
  2. 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

    Reply
  3. 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

    Reply
  4. 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

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.