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 breaks when using multiple joins #154

Closed
4 tasks done
degustadev opened this issue Jun 12, 2019 · 1 comment
Closed
4 tasks done

Count breaks when using multiple joins #154

degustadev opened this issue Jun 12, 2019 · 1 comment

Comments

@degustadev
Copy link

This is a bug.

Prerequisites

  • Are you running the latest version?
  • Are you reporting to the correct repository?
  • Did you check the documentation?
  • Did you perform a cursory search?

Description

When using multiple joins, the count of the table breaks and counts more results.
As you can see here is the response with count = 2
response

And here the rendered table with 1 result which is correct but it says 1 of 2 results, and with 1 result is ok, but when I have a lot of records it creates "ghost" pages to paginate a lot of non existing results.
table

I'm using this query:

$parts = explode('-', $params->filter->campaign);
$models = Product::select('user_contents.id as dtRowId', 'user_contents.id', 'user_contents.url', 'user_contents.visits_reach', 'user_contents.approved', 'user_contents.highlighted', 'user_contents.month', 'social_media_profiles.name')
                    ->distinct()
                    ->join('user_product', 'products.id', '=', 'user_product.product_id')
                    ->join('user_contents', 'user_contents.id', '=', 'user_product.user_content_id')
                    ->join('users', 'user_contents.user_id', '=', 'users.id')
                    ->join('social_media_profiles', 'user_contents.social_media_profile_id', '=', 'social_media_profiles.id')
                    ->whereNull('users.role_id')
                    ->where(function ($query) use ($parts, $params) {
                        $query->where([['products.year', '=', $parts[0]], ['products.month', '=', $parts[1]]])
                            ->orWhere('user_contents.month', '=', $params->filter->campaign);
                    })
                    ->with('user');

As you can see I'm using params to filter the results based on some external filters because I use manual tables. But well in the end should be the same.

Expected behavior

The package counts correctly the number of returned records

Actual behavior

The package counts incorrectly the number of returned records

@aocneanu
Copy link
Member

Distinct affects Laravel's query builder the same way groupBy does. Follow the discussion here

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

2 participants