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

Paging method, when calculating total, does not pass $columns #28844

Closed
tengfei31 opened this issue Jun 14, 2019 · 6 comments
Closed

Paging method, when calculating total, does not pass $columns #28844

tengfei31 opened this issue Jun 14, 2019 · 6 comments

Comments

@tengfei31
Copy link

  • Laravel Version: 5.8.8
  • PHP Version: 7.1.19
  • Database Driver & Version:

Description:

Function paginate, when calculating total, no $columns are passed. When I paginate parameters, the function getCountForPagination () does not pass $columns. Please take a look at it.

image
image
image

Steps To Reproduce:

@driesvints
Copy link
Member

Hmm, I'm not sure actually. For the query builder they are passed in and when I add it in the Eloquent builder myself the test suite still passes. So maybe we could add this in but I'm not sure what the side effects would be.

$total = $this->getCountForPagination($columns);

@staudenmeir can you see any downsides to passing the columns to the count query?

@staudenmeir
Copy link
Contributor

This doesn't work: #23259

Why do you want the count query to include the selected columns?

@tengfei31
Copy link
Author

Perhaps the user wants to compute by column,If it's of no use, you can ignore him. thanks

@staudenmeir
Copy link
Contributor

staudenmeir commented Jun 18, 2019

What's an example where you would want to use count(column) instead of count(*)?

The results can only be different if null values are involved: https://stackoverflow.com/a/3003533/4848587

@driesvints
Copy link
Member

Closing this issue because it's inactive, already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue and we'll re-open this issue.

@driesvints
Copy link
Member

FYI, I've sent in a PR to let the base query builder conform to this behavior as well: #28937

taylorotwell pushed a commit to illuminate/database that referenced this issue Jun 24, 2019
This is currently broken and will throw a SQL exception:

    DB::table('posts')->paginate(5, ['title', 'content']);

At the moment when you attempt to pass specific columns on the paginate method on the base query builder it will fail when you provide more than one column. SQL queries can only handle a count for a specific column and not multiple ones. That's why it's unwanted to pass along the columns parameter from the paginate method to the getCountForPagination method. Since we just want to do a count of the current result set a simple count with the '*' sign is enough. At the moment the Eloquent builder already handles pagination in this way.

It's noted by Staudenmeier that the only difference is when you provide a specific column to the getCountForPagination method is that it'll only count non-NULLs. However, it's not feasible to allow this to be done in combination with the paginate method because the columns param on the paginate method soley exists to filter which columns will be retrieved in the paginated result set (which is currently broken thus). See laravel/framework#28844 (comment)

I've added an extra integration test which reproduces the problem at hand. I've updated the tests in the DatabaseQueryBuilderTest class to reflect the changes made to getCountForPagination call.

This has been a long outstanding issue so hopefully this will prevent more issues from popping up.

Fixes laravel/framework#28890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants