owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
3.05k stars 390 forks source link

feat (Auditable): customized relationship for Attach, Detach and Sync #921

Closed wilianx7 closed 5 months ago

wilianx7 commented 7 months ago

Hello guys.

In my API I have a situation where I need to provide a BelongsToMany relationship with some conditions to Audit Attach, Detach and Sync.

This situation occurs because my pivot table has many pivot columns that need to be conditioned when I do an Attach/Detach/Sync.

Nowadays I can't do this, as these methods only accept the name of the relationship (without conditions). So I made this PR with a new optional parameter callback.

Example use case:

image

In this case, I just want to sync the serviceTypes relationship in my pivot table, where the pivot columns match the respective company_id and tenancy_id values. The other data in the pivot table remained unchanged.

parallels999 commented 7 months ago
wilianx7 commented 7 months ago

It's because I have published the master branch of my fork on Packagist. So I created another branch for this PR.

angeljqv commented 5 months ago

also syncWithPivotValues, syncWithoutDetaching are missing https://laravel.com/docs/9.x/eloquent-relationships#syncing-associations