-4

I want to echo 5000 user's documents from Firerstore to an HTML table using Laravel but am getting an error "call to undefined method Google\Cloud\Firestore\QuerySnapshot::count()". Please what am I getting wrong here:

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
4
  • you can simply replace $documents->count() with count($data)
    – N69S
    Commented Jul 8 at 12:03
  • doing this gave me an error message "Call to undefined method Google\Cloud\Firestore\QuerySnapshot::last()" please what else can i do to get this solved please
    – Tunde Adam
    Commented Jul 8 at 12:27
  • please i got this error "Call to undefined method Google\Cloud\Firestore\QuerySnapshot::end()"
    – Tunde Adam
    Commented Jul 8 at 12:36
  • I'll just add it in an answer
    – N69S
    Commented Jul 8 at 12:48

2 Answers 2

1

As mentioned by @AlexMamo, you are not handling a laravel collection but an instance of QuerySnapshot. The methods count() & last() dont exist on that class (or are defined with a different name, for example instead of count() you need to call size()).

You are already looping on it and have a convenient $data to do the same actions with

$nextVisible = null;
if (count($data) === 2000) {
    $lastEntry = end($data);
    $nextVisible = $lastEntry['id'];
}
2
  • Please it pointed an error on my view (Blade template) "Route [firestore] not defined."..
    – Tunde Adam
    Commented Jul 8 at 17:20
  • @SamuelEmeh that's another error unrelated to the question you asked. In all cases, you understand that you can't keep going like this and you need to learn how to develop/debug code or at least how to code on laravel.
    – N69S
    Commented Jul 9 at 8:03
0

You're getting the following error:

Call to undefined method Google\Cloud\Firestore\QuerySnapshot::count()

Because the QuerySnapshot class doesn't contain a count() function. On the other hand, the Query class contains a count() function. So if you want to count the documents that are returned by a query, call count() directly on the Query object.

1
  • please how can do that please
    – Tunde Adam
    Commented Jul 8 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.