Image
23 MAY

# How to Integrate Firestore with Laravel

In this article you will learn, how to connect firebase firestore with laravel application, how to use firestore database with laravel, how to use firebase service account to connect laravel with firestore in detail.

Just follow the few steps:

  • Create New Project in Firebase
    Create Firestore Database in Firebase
    Download Secret Json File From Firebase
    Create Laravel Project
    Download Firebase SDKs ( kreait/firebase-php ) and ( google/cloud-firestore ) Using Composer Dependency Manager
    Setup Firebase SDK ( kreait/firebase-php ) in laravel and Connect to Firebase
    Use Firestore Database in Laravel

Step 1. Create New Project in Firebase

Image
  • c. Now click on "Go to console". If you are creating an account first time on firebase you will get welcome page.
  • d. Click on "Create a project"
Image
  • If you have pre-build project in firebase, you will get this page. Here you can click on "Add Project"
Image
  • e. Enter your project name and click on "Continue"
Image
  • f. Click on "Continue." You will get below page.
Image
  • g. Just click on "Create Project" And your project will be created successfuly in just seconds. ( Step 1 Completed )
Image
  • After Project created successfully, you will get firebase dashboard.

Step 2. Create Firestore Database in Firebase

  • a. click on "Firestore Database" under "Build" Dropdown.
Image
  • b. click on "Create Database".
Image
  • c. Choose an option "Start in test mode"and click on "Enable" button.
Image
  • d. Select your Cloud Firestore location and click on "Enable" button
Image
  • Your firestore database is now created successfully. You will get below page. ( Step 2 Completed )
Image

Step 3. Download Secret Json File From Firebase

  • a. Hover on "Project Overview" in top left corner and click on "Project Settings".
Image
  • b. Below Page will open after clicking on "Project Settings". On that page click on "Service accounts"
Image
  • c. Scroll Down to this "Service accounts" page and click on "Generate new private key" button as shown below.
Image
  • d. Click on "Generate Key" button. A .json file will download in your system. Keep this file save as this be used in laravel project. ( Step 3 Completed )

Image

Step 4. Create Laravel Project

  • a. Create your laravel project by using below composer command in your command prompt or teminal.

    composer create-project --prefer-dist laravel/laravel FirebaseApp

    Make sure you have composer installed in your system before running this command.

    After sometime your project will be created with folder name "FirebaseApp" ( Step 4 completed )

Step 5. Download Firebase SDKs ( kreait/firebase-php ) and ( google/cloud-firestore ) Using Composer Dependency Manager

  • a. In your project root folder run this composer command to download (kreait/firebase-php) sdk.

    composer require kreait/firebase-php

    b. In your project root folder run this composer command to download (google/cloud-firestore) sdk.

    composer require google/cloud-firestore

    After Composer commands run successfully, firebase sdk will be downloaded in vendor folder of laravel project. ( Step 5 completed )

Step 6. Setup Firebase SDK ( kreait/firebase-php ) in laravel and Connect to Firebase

  • a. Rename .json file that you have downloaded from firebase service account to secret.json and paste it in your project public folder.

    Your Project public folder will have these files initially, If you are following this guide.

Image
  • b. You need to create factory instance in order to use firebase features like firestore database.

    Open web.php file and write following code, it create a new factory instance. You can also create controller but I'm using this for simplicity.

    <?php
    use Illuminate\Support\Facades\Route;
    use Kreait\Firebase\Factory;
    Route::get('/', function () {
        $factory = (new Factory)->withServiceAccount(public_path('secret.json'));
    });
    ?>

    Code Explain:

    use Kreait\Firebase\Factory; this line will import Factory class from vendor. Its a namespace

    secret.json (file that we have downloaded from service account of firebase)

    c. By using factory instance , we can create Firestore Database instance and make database opetations.

    Following code will create Firestore Database instance.

    <?php
    $firestore = $factory->createFirestore();
    $database = $firestore->database();
    ?>

    Now we have created our database instance. We are ready to make operations on it. ( Step 6 completed )

Step 7. Use Firestore Database in Laravel

  • a. Now we have firestore database instance by using this we can perform any database operations. For example: To make read operation, we can write following code

    <?php
    $documents = $database->collection('company')->documents();
    foreach ($documents as $document) {
        print_r('Document Id: ' . $document->id());
        print_r($document->data());
    }
    ?>

    Code Explain:

    $documents = $database->collection('company')->documents();

    By using above code we are fetching document instance of company collection.

    We can loop through to get single document instance.

    foreach ($documents as $document) { // }

    To fetch document id there is an id() function available on document instance.

    To fetch document data there is a data() function available on document instance.

    Image

    Final code to get all company data will be :

    <?php
    use Illuminate\Support\Facades\Route;
    use Kreait\Firebase\Factory;
    
    Route::get('/', function () {
        $factory = (new Factory)->withServiceAccount(public_path('secret.json'));
        $firestore = $factory->createFirestore();
        $database = $firestore->database();
        
        $documents = $database->collection('company')->documents();
        foreach ($documents as $document) {
            print_r('Document Id: ' . $document->id());
            print_r($document->data());
        }
    });
    ?>

Thanks for reading

3 COMMENTS

  1. image
    NicheTheme
    May 11, 2018 - at 11:00 am REPLY

    Fusce ornare mi vel risus porttitor dignissim. Nunc eget risus at ipsum blandit ornare vel sed velit. Proin gravida arcu nisl, a dignissim mauris placerat id. Vivamus interdum urna at sapien varius elementum.

    • image
      themesflat
      May 11, 2018 - at 11:00 am REPLY

      Fusce ornare mi vel risus porttitor dignissim. Nunc eget risus at ipsum blandit ornare vel sed velit. Proin gravida arcu nisl, a dignissim mauris placerat id.

  2. image
    NicheTheme
    May 11, 2018 - at 11:00 am REPLY

    Fusce ornare mi vel risus porttitor dignissim. Nunc eget risus at ipsum blandit ornare vel sed velit. Proin gravida arcu nisl, a dignissim mauris placerat id. Vivamus interdum urna at sapien varius elementum.

Leave a Comment