Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Firebase Firestore to laravel

I have installed laravel 5.8 and firebase project with firestore database.

 "name": "laravel/framework",
 "version": "v5.8.36",

Firestore database connected to android app. App fetches data good from Firestore. Than I want to create admin panel with laravel for android app and want to integrate laravel with that database.But can not do this.

What I did:

  1. installed php7.2

  2. installed laravel 5.8.*

  3. added php extension gRPC*

  4. Added gRPC as a Composer dependency composer require "grpc/grpc:^v1.1.0" in laravel project

  5. installed composer require google/cloud-firestore

  6. Generated Firebase Admin SDK json file and saved into storage folder in laravel

  7. Added this variable GOOGLE_APPLICATION_CREDENTIALS=/storage/files/progressive-yooung-team-firebase-adminsdk-ax7wi-d2a85ecabc.json (json file which generated firebase admin SDK) in .env file of laravel

  8. installed composer require kreait/firebase-php ^4.35

  9. Created Controller 'VarController' code:

    <?php
    
    namespace App\Http\Controllers;
    
    use Kreait\Firebase\Factory;
    
    
    class VarController extends Controller
    {
        public function index()
        {
            print_r("Output: 1");
            $factory = new Factory();
            print_r("Output: 2");
            $firestore = $factory->createFirestore();
            print_r("Output: 3");
            $database = $firestore->database();
    
            $userRef =  $database->collection('users');
            $snapshot = $userRef->document('Hus')->snapshot();
    
            if($snapshot->exists()) {
                printf('Document data:' . PHP_EOL);
                print_r($snapshot->data());
    
            }
            print_r("Output: 4");
        }
    }
    

Problem is, It does not fetch data from firestore document 'Hus' and its data exists:

users > Hus > name: "Husniddin"

I put print_r("Output: 1"), Output: 2, etc... in order to know where is problem. On screen I see only: Output: 1 Output: 2.

like image 478
Xose Avatar asked Dec 31 '19 10:12

Xose


People also ask

Can Laravel work with Firebase?

To access Firebase in Laravel, you will first have to configure Laravel to support it. This is done by installing a Firebase package for Laravel, such as kreait/firebase-php, which also supports Lumen projects. To install the package, run the command below.

Why connect Laravel with Firebase?

Connect Laravel with Firebase Real Time Database 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.

How to use the firebase local emulator suite?

The Firebase Local Emulator Suite emulates products for a single Firebase project. To select the project to use, before you start the emulators, in the CLI run firebase use in your working directory. Or, you can pass the --project flag to each emulator command.

How do I create a database in Firebase?

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.

What is Google Firebase?

What is Firebase? Firebase is a NoSQL document b database backend platform for developing Web (Android, and IOS) applications. Firebase easily store, sync, and query data for your mobile and web apps – at global scale. Firebase is a backend platform developed by Google for building mobile (Android, and IOS) web applications.


1 Answers

Also don't forget to import these .

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use \Kreait\Firebase\Database;
use Google\Cloud\Firestore\FirestoreClient;

After that , call the ServiceAccount() inside your function.

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/Firebase.json');
        $firebase = (new Factory)
        ->withServiceAccount($serviceAccount);
$firestore = new FirestoreClient([
            'projectId' => 'Your project name',
        ]);
        $collectionReference = $firestore->collection('users');

$documentReference = $collectionReference->document('Search element from document');
        $snapshot = $documentReference->snapshot();
like image 150
farooq Avatar answered Oct 21 '22 22:10

farooq