Skip to content

Conversation

@Amirhf1
Copy link
Contributor

@Amirhf1 Amirhf1 commented Apr 1, 2025

This PR fixes issue #55026 where pivot model observers don't work properly when using the withPivotValue method.

Description

When using a custom pivot model class with observers and calling withPivotValue on a relationship, the update and delete events don't fire as expected. This happens because withPivotValue adds conditions to pivotWheres, which prevents the code from using the custom pivot class path for updates and detaches.

Solution

  • Modified withPivotValue to mark where clauses that come from it with a from_pivot_value flag
  • Added a getNonPivotValueWhereClauses method to filter out where clauses that come from withPivotValue
  • Updated updateExistingPivot and detach methods to check for non-pivot-value where clauses instead of all where clauses
  • Modified newPivotQuery to properly apply where clauses but remove the flag before building the query
  • Added a test case to verify the fix works correctly

The solution ensures that pivot model observers work properly with withPivotValue while maintaining the filtering behavior required by the relation.

@Amirhf1 Amirhf1 changed the title Fix pivot model observers not working properly when using withPivotValue [12.x] Fix pivot model observers not working properly when using withPivotValue Apr 7, 2025
@taylorotwell
Copy link
Member

Closing in light of #55280

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.

2 participants