我想使用 Laravel 将 5000 个用户的文档从 Firerstore 回显到 HTML 表,但收到错误“调用未定义的方法 Google\Cloud\Firestore\QuerySnapshot::count()”。请问我这里出了什么问题:
public function history(Request $request)
{
$history = app('firebase.firestore')->database()->collection($this->tablenamePayment);
$lastVisible = null;
if ($request->input('last_visible')) {
$lastVisible = $history->document($request->input('last_visible'))->snapshot();
}
$query = $history->startAt($lastVisible ? [$lastVisible] : []);
$documents = $query->limit(2000)->documents();
$data = [];
foreach ($documents as $document) {
$data[] = [
'id' => $document->id(),
'name' => $document->get('name'),
'email' => $document->get('email'),
];
}
$nextVisible = null;
if ($documents->count() === 2000) {
$nextVisible = $documents->last()->id();
}
return view('admin.payment-history', compact('data', 'nextVisible'));
}
// View (Blade template):
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($data as $document)
<tr>
<td>{{ $document['id'] }}</td>
<td>{{ $document['name'] }}</td>
<td>{{ $document['email'] }}</td>
</tr>
@endforeach
</tbody>
</table>
@if ($nextVisible)
<a href="{{ route('firestore', ['last_visible' => $nextVisible]) }}">Next Page</a>
@endif
您收到以下错误:
调用未定义的方法 Google\Cloud\Firestore\QuerySnapshot::count()
因为 QuerySnapshot 不包含
count()
函数。另一方面,Query类包含一个count()函数。因此,如果您想计算查询返回的文档,请直接在 count()
对象上调用 Query
。