【问题标题】:Firestore connection issue in LaravelLaravel 中的 Firestore 连接问题
【发布时间】:2023-03-28 02:33:01
【问题描述】:

我正在使用 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 等,但没有解决这个问题。请任何人帮助我。

【问题讨论】:

  • 我希望@jeromegamez 会看到这篇文章并回答。那是您正在使用的库的维护者,他们过去曾回答过其他帖子。
  • 我不确定,但看起来您应该使用$database =$factory->createFirestore()->database(); 而不是使用工厂返回的对象。如果我查看文档和错误,看起来你有错误的对象,此时你应该有 Google\Cloud\Firestore\FirestoreClient 而不是 Kreait\Firebase\Factory,但我可能完全错了。添加一些东西=>这里是文档github.com/kreait/firebase-php/blob/5.x/docs/…

标签: php laravel google-cloud-firestore


【解决方案1】:

我已更改此代码

   $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

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 2019-02-03
    • 2017-11-20
    • 2013-07-21
    • 2020-04-19
    • 2019-04-09
    • 2015-04-08
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多