-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] fix: Model newCollection generics; feat: add HasCollection trait #52171
Conversation
Looking at your code, you add a trait, but then you aren't doing anything with the trait, it has zero effect, the newCollection is still in Model. |
The trait is for use in user land when a custom collection class is used as it better helps with generics/autocomplete/ide support, however, I suppose we could add the trait to the base model like this: |
if it is just for that, it doesn't slim down anything. I suggest either remove the trait or move generic logic into that trait. leaving Model as was. edit: Given that you also need logic inside those Collections to enforce a certain type: look at packages like this, who already does, what this PR wants to do, for you: https://packagist.org/packages/henzeb/laravel-typed-collection |
I've updated the Model to use the HasCollection trait, but I can always revert if this is not desired |
The trait is still not doing anything except for extending the original newCollection method. The trait should not have to be part of Model by default. by default Model uses the default Collection class. But only when adding that trait, one should be able to set a specific Collection classname. No code changes in Model, just a new trait. |
Thanks @taylorotwell! |
Hello!
This PR fixes some issues with the Model's newCollection generic (phpstan/phpstan#11354) ---it appears that due to covariant/invariant constraints the method must return only static models.
I also added a
HasCollection
trait similar to theHas{Builder,Factory}
traits that help slim down the models:Thanks!