-
-
Notifications
You must be signed in to change notification settings - Fork 859
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
withCount relation with Where results in surplus bindings in the count query #1600
Comments
@kronthto @fschalkwijk I think maybe we can include Can you please try to update this function: /**
* Check if builder query uses complex sql.
*
* @param \Illuminate\Database\Query\Builder $builder
* @return bool
*/
protected function isComplexQuery($builder)
{
return Str::contains(Str::lower($builder->toSql()), ['union', 'having', 'distinct', 'order by', 'group by', 'join']);
} UPDATE |
User::query()->select()->withCount('activeTokens') |
I was able to replicate the issue and @fschalkwijk PR fixes this. Also to further avoid the same issue, I added a PR #1737. Should be fixed on the next release later. |
Summary of problem or feature request
When using
withCount
with a relation that has own bindings (Wheres), the filtered count query bindings do not match the query.This can result in
recordsFiltered
in the response being off or often just zero.Using the datatables frontend JS, the result count is wrong (
Showing 0 to 0 of 0 entries (filtered from 30,280 total entries)
), which subsequently causes pagination buttons to disappear. The actual data however is shown and ordering also works.May be related to #1471 however search/ordering is working in my case.
Code snippet of problem
datatables call:
relation on
User
:When using the
globalSearch
the following query is executed + bindings:This is OK and works. I assume this is the data query.
However, it then goes through
prepareCountQuery
(to fetch the total filtered count), which overwrites theselect
part:laravel-datatables/src/QueryDataTable.php
Lines 148 to 151 in fddf0d7
This results in the following query:
Would also look good, but the bindings are the same as above. However, the subselect with 2 params was overwritten, so there are too many bindings (leftovers) and they get matched to the wrong params.
System details
The text was updated successfully, but these errors were encountered: