Skip to content
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

Closed
hailwood opened this issue Sep 17, 2013 · 15 comments
Closed

[Proposal] Fire events on relationship creation #2303

hailwood opened this issue Sep 17, 2013 · 15 comments

Comments

@hailwood
Copy link
Contributor

Presently there is no way to know when a relationship has been created via

Model::find(1)->something->save($anotherSomething);

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!

@maknz
Copy link

maknz commented Sep 17, 2013

+1, this would be nice.

@Kindari
Copy link

Kindari commented Sep 17, 2013

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 relationship.creating: foo_bar where foo and bar are related. Can these be customized? What about polymorphic relations?

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.

@hailwood
Copy link
Contributor Author

ModelA::find(1)->modelbs->save($anotherModelB); 

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 ModelA you would have hasMany('ModelB');

HasMany would be extended to have (as an example of the save method)

Event::fire('eloquent.related: '.get_class($this->parent), $model);

You would subscribe to the relationship being created on ModelA in this case listening on the eloquent.related: ModelA event. This structure allows you to react to any relationship being created on ModelA by passing the related model as the payload of the event the event subscriber can respond only if the relationship is of a specific child.

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?

@Kindari
Copy link

Kindari commented Sep 17, 2013

Simple example, https://gist.github.com/Kindari/6591313

@bonzai
Copy link
Contributor

bonzai commented Sep 25, 2013

👍

This especially would be helpful for many-to-many relationships (with pivot table), where no event is executed after inserting/deleting model.

@taylorotwell
Copy link
Member

This is honestly getting pretty complicated. I think Kindari's gist provides a decent work-around.

@NoelDavies
Copy link

@taylorotwell this doesn't cover pivot tables. That's the issue here, no?

@Gwill
Copy link
Contributor

Gwill commented May 11, 2015

This doesn't cover pivot tables +1

@camih
Copy link

camih commented Dec 11, 2015

+1

1 similar comment
@mustafaaloko
Copy link
Contributor

+1

@austenpayan
Copy link

+1 for pivot tables

@Ardakilic
Copy link

+1

@fico7489
Copy link

fico7489 commented Nov 9, 2017

I created something similar for belongsToMany relations (pivot tables) : https://github.com/fico7489/laravel-pivot

@amitshahc
Copy link

amitshahc commented Sep 11, 2019

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.

@ronnyandre
Copy link

👍 from me as well.

Example:

Two models, a Project has many Expenses. If a project uses a foreign currency, say Euro, and you have expenses for that project in US Dollars, and you want to convert the expense to the projects local currency upon saving.

This isn't that easy to do as the relation isn't available for events like creating, creating, saving and saved for new expenses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests