I have a laravel project, and I need to make some calculations immediately after I save a model and attach some data to it.
Is there any event that is triggered in laravel after calling attach (or detach/sync)?
Advertisement
Answer
Steps to solve your problem:
- Create custom BelongsToMany relation
- In BelongsToMany custom relation override attach, detach, sync and updateExistingPivot methods
- In overriden method dispatch desired events.
- Override belongsToMany() method in Model and return your custom relation not default relation
and that’s it. I created package that already doing that: https://github.com/fico7489/laravel-pivot