-
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
Introduces UseFactory attribute #54065
base: 11.x
Are you sure you want to change the base?
Conversation
I think I'm on board overall. Any reason you didn't just choose |
@taylorotwell No particular reason other than for an explicit name to tell the developer exactly what it does, and to follow a more action-focused naming convention of the other attributes, e.g. Also, probably just React leaking into my brain 😅 |
One might name it |
@taylorotwell Would you like me to change to |
Hmm, got it. Good point on ScopedBy, etc. I would probably name this |
Well, it's possible—it could just end up being a PITA to keep in mind to alias one of them 😂 |
@taylorotwell Yeah that was my conclusion as well. Which, is how I arrived at Naming is hard! |
Description
This PR introduces a new UseFactory attribute that provides a more elegant way to associate model factories with Eloquent models using PHP 8.0+ attributes. This is especially useful for factory registration for models that are outside of the typical
App\Models
namespace and improves code readability (in my opinion).Usage
Instead of defining a new method for
newFactory()
, we can simply allow developers to add the Attribute.Automatic inverse detection
When using this attribute, the factory for the given model will automatically be set, so there is no need to define a
protected $model = \App\SomeFeatureDomain\Models\Comment::class
from our example above.Completely Optional & Fully backwards compatible
This logic only runs on classes that implement this attribute. Developers are still free to define static
newFactory
methods as they have before.Priority Order
The factory resolution follows this priority:
$factory
property if definedUseFactory
attribute if presentRequirements
Testing
This PR includes tests that verify: