Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Because of the issues I was having in #48 and the issue @ericdiviney was having in #16, this PR adds a call to a method on the underlying model class, if it exists.
The Laravel
->sync()
method returns an array of changes as can be seen here: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php#L90.This PR takes the attribute name, converts to camelCase and adds
Synced
to the end, and attempts to call that method if it exists on the model.This is useful in the following circumstance (as outlined in #48).
If I have an
Author
model:I can now add a method
relatedAuthorsSynced()
to the model which can accept the list of changes that have happened:Using this information I could now sync the inverse side really easily. In the case of #16 the model could either fire a new event to be observed, or the code from the observer could be brought into the model.
Users are free to ignore the method if they want the existing behaviour and the line referenced in #48 (https://github.com/dillingham/nova-attach-many/blob/master/src/AttachMany.php#L46) can continue working as it is, ensuring that the model has been saved successfully before attempting the sync.
I've also updated the README to document the change.