Skip to content
Advertisement

Eloquent attach/detach/sync fires any event?

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:

  1. Create custom BelongsToMany relation
  2. In BelongsToMany custom relation override attach, detach, sync and updateExistingPivot methods
  3. In overriden method dispatch desired events.
  4. 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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement