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

[5.3] Eloquent : withCount() overrides the $columns on get() #15576

Closed
shadoWalker89 opened this issue Sep 23, 2016 · 10 comments
Closed

[5.3] Eloquent : withCount() overrides the $columns on get() #15576

shadoWalker89 opened this issue Sep 23, 2016 · 10 comments

Comments

@shadoWalker89
Copy link
Contributor

shadoWalker89 commented Sep 23, 2016

  • Laravel Version: 5.3.6
  • PHP Version: 5.6.15
  • Database Driver & Version: Mysql 5.6.31

Description:

When using withCount() from eloquent Builder it ignores the $columns passed to the get() method and select all the columns.

Steps To Reproduce:

User::withCount('someRelation')->get(['id', 'name']);
@themsaid
Copy link
Member

Ping @barryvdh

withCount() selects * on the parent query, it prevents any further column selection, is there a way around this?

@barryvdh
Copy link
Contributor

That is what I mentioned in the PR as possible problem, which I don't have a nice solution for. We need to add a select, if we just add the new column, the default '*' gets ignored (which is more common). See replies from here: #13414 (comment)

I think it should work when you explicitly use a select() call before the count.

User::select(['id', 'name'])->withCount('someRelation')->get();

@themsaid
Copy link
Member

Yes it works this way.
@shadoWalker89 Check the reply above.

@shadoWalker89
Copy link
Contributor Author

Yes it works the way barryvdh said.
Maybe you could add the fix for this as 5.4 milestone ?

@barryvdh
Copy link
Contributor

Or perhaps add this to a note to the withCount docs?

@Luis-Goncalves-Searchprof

You could also use the "addSelect" instead of "select", that way you can put it after the "withCount".

User::withCount('someRelation')->addSelect(['id', 'name'])->get();

@clytras
Copy link

clytras commented Jun 20, 2021

There should be a note regarding this. I spent over an hour trying to figure this out why get([fields]) isn't working with withCount.

@Luis-Goncalves-Searchprof addSelect is not working after withCount.

@driesvints
Copy link
Member

@clytras we're always welcoming PR's that improve the docs 👍

@clytras
Copy link

clytras commented Jun 20, 2021

@driesvints I'm not aware of the Notes Markdown format that I should use to add one in Counting Related Models.

If you do, please provide it here and I will make a PR for this.

@rpmcmurphy
Copy link

$query->paginate() overrides the select() method and brings everything. Any way around for paginated results?

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

7 participants