Framework: [Proposal] Fire events on relationship creation

Created on 17 Sep 2013  路  15Comments  路  Source: laravel/framework

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!

Most helpful comment

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

All 15 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 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.

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?

:+1:

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

+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.

:+1: 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anahkiasen picture Anahkiasen  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

ghost picture ghost  路  3Comments

iivanov2 picture iivanov2  路  3Comments