php - 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 等,但没有摆脱这个问题。任何人请帮助我。
解决方案
我已更改此代码
$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
推荐阅读
- react-navigation - React-navigation 材质顶部选项卡指示器样式
- javascript - 2D Canvas 上 createGraphics WEBGL 中的 orbitControl
- php - PDFlib 在定义的 fitbox 底部放置表格
- php - Coinmarketcap PHP API 没有返回数据
- python - Paho 发布在 Docker 上的第一个主题后停止
- wordpress - WordPress:网站看起来不像演示
- parameters - Google DataStudio:链接参数以控制下拉列表
- node.js - puppeteer 错误:找不到选择器的节点:.olp-text-box
- java - 无法运行项目灰色运行按钮 - Intellij
- android - Android Telecom Manager 隐藏/禁用添加呼叫和发送消息按钮