Cloud Firestore¶
This SDK provides a bridge to the google/cloud-firestore package. You can enable the component in the SDK by adding the package to your project dependencies:
composer require google/cloud-firestore
Note
The google/cloud-firestore
package requires the gRPC PHP extension to be installed. You can find installation
instructions for gRPC at github.com/grpc/grpc. The following
projects aim to provide support for Firestore without the need to install the gRPC PHP extension, but have to
be set up separately:
Before you start, please read about Firestore in the official documentation:
Initializing the Firestore component¶
With the SDK
$firestore = $factory->createFirestore();
With Dependency Injection (Symfony Bundle/Laravel/Lumen Package)
use Kreait\Firebase\Contract\Firestore;
class MyService
{
public function __construct(Firestore $firestore)
{
$this->firestore = $firestore;
}
}
With the Laravel app()
helper (Laravel/Lumen Package)
$firestore = app('firebase.firestore');
Getting started¶
$database = $firestore->database();
$database
is an instance of Google\Cloud\Firestore\FirestoreClient
. Please refer to the links above for
guidance on how to proceed from here.
Use another Firestore Database¶
If you don’t specify a database, the Firestore Client will connect to the (default)
database.
If you want to connect to another database, you can specify its name with the factory. You can work with multiple Firestore Databases simultaneously.
use Kreait\Firebase\Factory;
$factory = new Factory();
$defaultDatabase = $factory
->createFirestore()
->database();
$otherDatabase = $factory
->withFirestoreDatabase('another-database')
->createFirestore()
->database();
$thirdDatabase = $factory
->withFirestoreDatabase('third-database')
->createFirestore()
->database();
Add Firestore configuration options¶
You can add additional configuration options for the Firestore Client used by the Firestore component:
use Kreait\Firebase\Factory;
$factory = new Factory();
$firestore = $factory
->withFirestoreClientConfig([...])
->createFirestore();
You can find all configuration options in the source code of the FirestoreClient
class of the
official Google Firestore PHP library.
In fact, the withFirestoreDatabase()
method is a shortcut for the withFirestoreClientConfig()
method:
use Kreait\Firebase\Factory;
$factory = new Factory();
$firestore = $factory->->withFirestoreDatabase('another-database');
// is a shortcut for
$firestore = $factory->withFirestoreClientConfig(['database' => 'another-database']);