Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joostelders opened this issue Nov 12, 2021 · 0 comments
Open

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

joostelders opened this issue Nov 12, 2021 · 0 comments

Comments

@joostelders
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant