-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Add/fix support for distinct selects when using pagination #4088
Conversation
I need better examples of what was fixed here. Post the incorrect SQL that was generated before and the Eloquent or query builder query used to generate it. Then post the correct SQL that is generated now. |
// We have to check if the value is "null" so that the count function does | ||
// not attempt to count an invalid string. Checking the value is better | ||
// here because the count function already has an optional parameter. | ||
if ( is_null($columns)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the extra space: if (is_null($columns))
.
@taylorotwell Good point - that is admittedly pretty vague. Eloquent: Header::select('query_serial_num', 'serial_num', 'motor_model_no')
->distinct()
->where('query_serial_num', 'like', $serial.'%')
->paginate(self::RESULTS_PER_PAGE); Before fix: # Count Query
select count(*) as aggregate from `header` where `query_serial_num` like ?
# Results Query
select distinct `query_serial_num`, `serial_num`, `motor_model_no` from `header` where `query_serial_num` like ? limit 50 offset 0 After fix: # Count Query (changed)
select count(distinct `query_serial_num`, `serial_num`, `motor_model_no`) as aggregate from `header` where `query_serial_num` like ?
# Results Query (unchanged)
select distinct `query_serial_num`, `serial_num`, `motor_model_no` from `header` where `query_serial_num` like ? limit 50 offset 0 |
I have this issue also. |
We would also love to see this problem fixed! A brief test of uzegonemad's changes shows they do indeed seem to fix it. With the current version of laravel (4.1) we are having to work around this problem by resorting to using DB::raw() queries for accurate pagination results when distinct() is used. Hopefully a change can make it into 4.2! |
@taylorotwell I understand you're busy but do please let me know if there's any other information you need from me. Thanks! |
This broke the following situation and had to be reverted. |
Thanks for the heads up, maybe you have a better solution to the problem? |
I know this is very old, but I still seem to have the same issue in laravel 5.1. |
This adds/fixes the ability to use
select()->distinct()->paginate()
. Previously, the count would be off because the count query disregardeddistinct()
.Fixes #3191