[11.x] Update detachUsingCustomClass to trigger delete event with full model record #51776
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.
TLDR: This update allows the any listener to the delete event on a pivot model record to receive the full set of columns (including the PK) instead of just the foreign keys.
Fully compatible with 10.x branch as well.
Long Description: I am working on a feature to audit pivot column changes. Attach and detach work fine to create audits. However, detach (the delete event) failed to create an audit record because my audits table does not allow null values for auditable primary key value. The issue is that the detach section was creating a new pivot record only with the foreign keys leaving out all other columns from the delete event.
While debugging I dug into the history of Laravel supporting pivot records triggering events and this change brings the framework more in line with how the
updateExistingPivot
method works when using a pivot model in theupdateExistingPivotUsingCustomClass
method.I also made the delete null safe in case the record has already been deleted by another process it doesn't throw a "call to member function delete() on null" as was fixed on this pull request for the pivot update section -> #29362
Relevant links to other PRs or commits related to this change:
getCurrentlyAttachedPivots
method inupdateExistingPivotUsingCustomClass
-> e31d131Happy to add necessary test but generally speaking this change should match all existing tests.
Thank you for your consideration!
Abraham