Skip to content

[12.x] Fix pivot model events not working when using the withPivotValue#55280

Merged
taylorotwell merged 2 commits into
laravel:12.xfrom
amir9480:fix_pivot_events_not_working
Apr 11, 2025
Merged

[12.x] Fix pivot model events not working when using the withPivotValue#55280
taylorotwell merged 2 commits into
laravel:12.xfrom
amir9480:fix_pivot_events_not_working

Conversation

@amir9480

@amir9480 amir9480 commented Apr 5, 2025

Copy link
Copy Markdown
Contributor

Fixes #55026.

  • For updateExistingPivot, there is no extra cost in terms of performance; it's already sending a database query for fetching data when using the updateExistingPivot method for custom classes. (InteractsWithPivotTable.php#L242)
  • For detach, a query like updateExistingPivot will be run before deleting, only if the user uses a custom pivot class and withPivotValue (or wherePivot), otherwise, it's working exactly as it was working before.
  • The previous version of updateExistingPivot used the collection where method instead of query builder where method, so I added a new method to get currently attached pivot models with related keys filter, so the where method will run by query builder instead of collection for better performance and not fetching data that is not needed for update/delete pivots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding pivot values causes the pivot model observer not work properly

2 participants