CallBeforeCreating event in eloquent factory #56322
Unanswered
victor-priceputu
asked this question in
Ideas
Replies: 1 comment 1 reply
-
Basically you need to modify the code base just to be able to unit test? If yes then you have precedent and it could be implemented. We saw code in laravel written just for unit tests that is useless in production. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
When the factories for models are used,
make
saves the relationships the model is dependent on and set in the definition, but not the current model. This in a way makes sense, because how else would you potentially have the IDs for those models.The issue I have is that I need the relations to not be saved (the project I am working on is using UUIDs as IDs, and they are manually generated, plus we do not have a database for unit tests, only for feature tests because 'best practice, you should not need one for unit tests'), and I need a relationship in a post save event which breaks all tests.
We are using the factories everywhere, and would like to be able to easily set the relation in the factory, to ensure we always have data integrity, and not have to manually set in every test.
For this I would like to request a new callback, similar to
afterMaking
andafterCreating
that is only called increate
, but after the call tomake
, basically right beforeframework/src/Illuminate/Database/Eloquent/Factories/Factory.php
Line 308 in 2c682e4
framework/src/Illuminate/Database/Eloquent/Factories/Factory.php
Line 312 in 2c682e4
store
, to allow more data manipulation to be done between make and create, in preparation for things like saving events.I know this wall of text may not make much sense, please feel free to ask for clarifications.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions