首页 > 解决方案 > Laravel 中的 Firestore 连接问题

问题描述

我正在使用 firebase 数据库在 laravel 上开发管理面板。它在实时数据库中运行良好,但在 firestore 的情况下,它会在 collection() 上给出错误。

我正在导入的课程是

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

这是运行良好的实时数据库的代码。

   $factory = (new Factory)->withServiceAccount(__DIR__.'/errand.json');
    $database = $factory->createDatabase();
    $reference = $database->getReference('tokens');
    $value = $reference->getValue();

但是在 collection() 的 firestore 中出现了问题,它的代码是

   $factory = (new Factory)->withServiceAccount(__DIR__.'/foodTruck.json');
   $database =$factory->createFirestore();
   $docRef = $database->collection('drivers');
   $snapshot = $docRef->snapshot();

这是 laravel 抛出的错误:

Error
Call to undefined method Kreait\Firebase\Factory::collection()

我还安装了 gRPC 等,但没有摆脱这个问题。任何人请帮助我。

标签: phplaravelgoogle-cloud-firestore

解决方案


我已更改此代码

   $factory = (new Factory)->withServiceAccount(__DIR__.'/foodTruck.json');
   $database =$factory->createFirestore();
   $docRef = $database->collection('drivers');
   $snapshot = $docRef->snapshot();

对这个

    $factory = (new Factory)->withServiceAccount(__DIR__.'/mobznew.json');
    $database = $factory->createFirestore()->database();
    $usersRef = $database->collection('users');
    $snapshot = $usersRef->documents();
    foreach ($snapshot as $user) {
    $age = array("uid"=>$user['userId'],"email"=>$user['email']);
    $data[]=$age;        
    }

    echo json_encode($data);

这对我来说很好。特别感谢@Frankich


推荐阅读