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:
installed php7.2
installed laravel 5.8.*
added php extension gRPC*
Added gRPC as a Composer dependency composer require "grpc/grpc:^v1.1.0"
in laravel project
installed composer require google/cloud-firestore
Generated Firebase Admin SDK json file and saved into storage folder in laravel
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
installed composer require kreait/firebase-php ^4.35
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.
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.
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.
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.
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 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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With