-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 custom collection support for get and all methods #903
Conversation
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.
After testing this PR on real codebase I've the following feedback:
- The
all
method without a custom collection is already correctly resolved due to laravels phpdoc on\Illuminate\Database\Eloquent\Model::all
:The PR in the current state however adds it all the time to models although redundant. If would only be necessary if there truly is a* @return \Illuminate\Database\Eloquent\Collection|static[]
newCollection
method returning a different collection - The
get
method, though technically not exactly the same, however behaves the same when using theide-helper generate
command- in which case
Model::get
resolves to the definition within the_ide_helper.php
file also correctly:
* @return \Illuminate\Database\Eloquent\Collection|static[]
- in which case
Although I'm aware it's up to everyone which parts of the package to use, you get only most of out of it when combining things.
Therefore my suggestion, for both new phpdoc methods: only generate them if the returned class is not \Illuminate\Database\Eloquent\Collection
This cuts down a lot of noise on existing models which have no such customization.
What do you think about that?
@mfn Thanks for the feedback. I have added the change so that it only adds these when using a custom collection. |
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.
Nice that you also used static[]
, totally makes sense.
Also tested in on a real project, works as expected and I'm really glad it does not generate the "unnecessary noise" for those not actually using custom collections!
IMHO this is good to merge @barryvdh !
Co-Authored-By: Markus Podar <[email protected]>
@mfn Is this likely to get merged and released soon? I am using a fork atm and am happy to carry on doing so but there are a lot of other nice PRs on here 👍 |
I can't say, I don't have more stakes here than you 🤷♀️ |
Thanks! |
@mfn @barryvdh Just tried in a few applications and we get custom collection code completion on Foo::get()
Foo::all() Also if you build a query using Foo::query()->where('bar', 'baz')->get() // completes to custom collection But if I build a query directly from the model there is no completion even though it does return the custom collection instance 😿. Foo::where('bar', 'baz')->get() // completes to Builder[]|Collection Can you see an obvious way to solve this? I am willing to PR another go but I can't see how this can be done without adding docs for all the builder methods in the |
Related to: #273 #171
Currently custom collections are only provided code completion when accessed via a relation that returns a
Collection
(HasMany
,BelongsToMany
etc.).This PR adds code completion on the
get
andall
methods. Tests included.