laravel-eloquent-join icon indicating copy to clipboard operation
laravel-eloquent-join copied to clipboard

Count without first calling ->get() always returns 1

Open joostelders opened this issue 3 years ago • 0 comments

Hello! This package is awesome but I'm struggling to use it on large tables.

Doing a count() before calling the get() on a QueryBuilder, will always return 1 instead of the actual count. So whenever you apply a whereJoin on a very large table, you first need to call get() before being able to do a count().

This is causing issues for me when I want to include the total count of matching records (for pagination) before applying a limit and offset.

public function get()
{
    $query = User::query()
        ->whereJoin('comments.text', 'LIKE', 'This is comment number 999%');

    return response()->json([
        'count_without_get' => $query->count(), // returns 1
        'count_with_get' => $query->get()->count() // returns 11
    ]);
}

I've created an example application to demonstrate the issue, run composer test for a simple test case. https://github.com/joostelders/eloquent-join-count-example

joostelders avatar Nov 12 '21 15:11 joostelders