-
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
[5.6] Add 'after' callback for model factories #23495
[5.6] Add 'after' callback for model factories #23495
Conversation
I really like this, but I think it would be more relevant if you could show an use-case that cannot be achieved by Eloquent Created Event. |
Yah, part of me feels this would be more appropriate to do in your |
Hmm, maybe I'm just missing an existing opportunity here. I specifically ran into wanting to do this in testing where I needed to have a team, which has users and belongs to an owner. Existing functionality lets me define the owner of the team when I fake up a team; but that owner isn't a member of the team until they're also added to the pivot table. The team isn't valid without an owner_id. Similarly, the team isn't valid if the owner isn't also a member. I don't think the Eloquent Created event applies to this situation, unless there is an intuitive way I'm missing to add this event listener during test setup? I will concede that I can just attach the user to the team as the second line every time I create a new team. But I literally do it every test that has a team, they are directly coupled which is why I think this is appropriate. |
+1 I was thinking about making a PR that does the exact same thing :D hope it gets merged, this is really what i have been missing for my tests |
I like this a lot. The case that is missing is applying it conditionally like with a state. If it is something that should always be done when that model is created use events. I have room into this car where I want to make records and children for tests. The test code ends up with a ton of boilerplate. An API that let you bind after callbacks to a specific state would be awesome. Something like Shouldn't the action be 'created' ? |
This is definitely a much cleaner solution than the current approach of having |
I get the action names now. I would rather see methods named afterCreate afterMake |
Made some changes to this. Methods are afterMaking and afterCreating... also allow you to register multiple callbacks per model. |
I still want to see them per state :-\ |
Would you accept a pr that allowed you to set them by state? |
This should be reflected in documentation. |
This PR implements the ability to add an
after
callback for model factories. The callback allows the user to define an action to be run after the model is created or made. A common use case for this behavior is to associate a user who is the owner of a group or team with the group or team, or to create an associated profile record for a new user model.If the action is omitted from the after function call, 'create' is assumed. Simplified ussage documentation snippet below:
After Callbacks
After callbacks allow you to define additional required logic to run after a model is created by a model factory. For example, your User model might have a required profile model, or you have a group or team model that has an owner but also requires the user to be added to a pivot table.
By default after callbacks are run after a model is created; if you want to run a callback after a make() call you can define the action the callback is to be run after as a third argument.