[10.x] Make sure pivot model has previously defined values #46559
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.
This is a retry of #46522, however I try to be more clear about the issue here, also added tests with another approach.
Due to Taylor's comment, my previous PR was closed, because the
withPivotValue
was mixed up with thewithPivot
method, however they are two diffent methods. (Also, as I see, thewithPivotValue
is not even documented, so if you wish, I gladly open a PR to document it, because I find it super handy.)The
withPivotValue
defines awherePivot
statement and sets a pivot value when saving the relation. So it is used when saving AND retreiving relations.The
withPivot
method just retrieves a pivot column, it does not even define a where statement.When using the
withPivotValue
, that value is not being set when making a new pivot model using the relationship itself ($model->relation()->newPivot()
).It means, if a developer is using a custom pivot model, and that model has default attribute values using the
$attributes
array, when saving the relationship, thewithPivotValue
won't work, however it should override the proper attribute in the$attributes
array.