Back

How to observe Many to Many relation attach()/sync() action?


How to observe Many to Many relation attach()/sync() action?

For this: I have Tag model and Post model, their raletion is Many to Many. I want to $tag->count += 1 when Post attach() with a Tag.

How to do that?

beanmoss replied 2 years ago

I think you can use http://laravel.com/docs/5.0/eloquent#model-events for this situation.

Gwill replied 2 years ago

but this is only about Model event, not relation event.

beanmoss replied 2 years ago

I suggest you create the model events from creating, created, updating and updated. check which event is fired during the attach()/sync().

jarektkaczyk replied 2 years ago

@Gwill you're right, there are no such events. Here's something you may use http://stackoverflow.com/questions/28925292/eloquent-attach-detach-sync-fires-any-event

fico7489 replied 1 week ago

Take a look on this package : https://github.com/fico7489/laravel-pivot


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us