-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[Proposal] Fire events on relationship creation #2303
Comments
+1, this would be nice. |
How would this be registered exactly, for every relationship? (therefore having to check some logic to determine what two models are being related)? Or what about the relationship name? What if that name is shared between multiple models? Or what about Let me also say this can currently be done with normal model events and simply checking if the relating column was modified. That being said there are certainly use cases where something like this would be better, but its a very generic idea and thought I would ask a bunch of questions to get things started. |
The way I see it is that the model the relationship is being created on (in this case ModelA) would fire the event. So in HasMany would be extended to have (as an example of the save method)
You would subscribe to the relationship being created on This alleviates the issue of multiple models sharing the same relationship and afaik this would also deal with polymorphic relationships? @Kindari Mind sharing how this can be done with the present model events? |
Simple example, https://gist.github.com/Kindari/6591313 |
👍 This especially would be helpful for many-to-many relationships (with pivot table), where no event is executed after inserting/deleting model. |
This is honestly getting pretty complicated. I think Kindari's gist provides a decent work-around. |
@taylorotwell this doesn't cover pivot tables. That's the issue here, no? |
This doesn't cover pivot tables +1 |
+1 |
1 similar comment
+1 |
+1 for pivot tables |
+1 |
I created something similar for belongsToMany relations (pivot tables) : https://github.com/fico7489/laravel-pivot |
I encountered this missing feature several times and feels like this is basic need in any project flow. For example, while creating appointment model, i need to calculate the amount of the services/packages (which are relation models). then on create observer doesn't make any sense as these relationships are not yet created when it fires. |
👍 from me as well. Example: Two models, a This isn't that easy to do as the relation isn't available for events like |
Presently there is no way to know when a relationship has been created via
This proposal puts forward the idea of firing
relationship.creating
,relationship.created
,relationship.destroying
,relationship.destroyed
events in the same manner as saving a model.This allows us to do awesome things like setup another relationship automatically when this relationship is setup, or send a "Dude someone joined your crew" email when a new relationship is added!
The text was updated successfully, but these errors were encountered: