Firestore Admin SDK for Laravel 是一个用于与 Firebase Firestore 数据库进行交互的 Laravel 扩展包。它提供了一个方便的方式来管理 Firestore 中的数据,包括创建、读取、更新和删除文档。
要在 Laravel 项目中安装 Firestore Admin SDK,请按照以下步骤操作:
composer require kreait/laravel-firebase
config/app.php
文件中的 providers
数组中添加服务提供者:'providers' => [
// ...
Kreait\LaravelFirebase\KreaitLaravelFirebaseServiceProvider::class,
],
config/app.php
文件中的 aliases
数组中添加门面:'aliases' => [
// ...
'Firebase' => Kreait\LaravelFirebase\Facades\Firebase::class,
],
php artisan vendor:publish --provider="Kreait\LaravelFirebase\KreaitLaravelFirebaseServiceProvider"
在 .env
文件中设置 Firebase 项目的凭据:
FIREBASE_CREDENTIALS=/path/to/your/firebase/credentials.json
以下是一些使用 Firestore Admin SDK for Laravel 的示例:
use Firebase.firestore;
$collection = firestore()->collection('users');
$documents = $collection->documents();
foreach ($documents as $document) {
echo $document->id() . ': ' . $document->data() . PHP_EOL;
}
use Firebase.firestore;
$collection = firestore()->collection('users');
$newDocument = $collection->newDocument();
$newDocument->set([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
$reference = $newDocument->create();
echo 'Created document with ID: ' . $reference->id();
use Firebase.firestore;
$collection = firestore()->collection('users');
$document = $collection->document('user_id');
$document->update([
'name' => 'Jane Doe',
'email' => 'jane.doe@example.com',
]);
echo 'Document updated';
use Firebase.firestore;
$collection = firestore()->collection('users');
$document = $collection->document('user_id');
$document->delete();
echo 'Document deleted';
领取专属 10元无门槛券
手把手带您无忧上云