-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[12.x] Feat: ability to explicitly exclude factory relationships #56396
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
base: 12.x
Are you sure you want to change the base?
[12.x] Feat: ability to explicitly exclude factory relationships #56396
Conversation
Does this work with morphable relationships? |
@cosmastech yes, test case uses a polymorphic relationship beside it should work for all the relationships that factory supports. |
Why not added to the |
@taylorotwell, I got your point that way the state of I've added a commit for this so re-review this please. |
What if a model has multiple relationships of the same model type... think |
At first, I considered excluding relations based on attribute names, but I didn’t choose that approach as it felt inconsistent with the method name. I can think of two possible solutions:
|
first solution is good to me. Second one is a little mess and redundant. |
The
withoutParents
method is definitely a useful method but it's still missing some crucial capabilities. It's actually quite common for a table to have multiple relationship columns, where only some are nullable. In most cases, at least one relationship is mandatory.For example: imagine an Employee model that has relationships with both Department and Allowance models. Interns aren’t assigned to any department until they complete a grace period, but they must have allowances(no one works for free).
In this case, I need to create an employee (intern) record without creating a department, but with an allowance. Unfortunately,
withoutParents
can’t handle this; it’s all or nothing. That makes it insufficient for this kind of use case.However, with this new enhancement we can define which relationships should be excluded and
withoutParents
method will be completed. I understand concerns about framework bloat, but I reckon this functionality will be useful to others too. And the best part: I implemented it without introducing any new method except just a small internal variable andwithoutParents
accept optional array to define the relationships that should be excluded.Employee factory
Create a employee with a allowance without a department