[10.x] Adds the firstOrCreate
and createOrFirst
methods to the HasManyThrough
relation
#48541
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.
Added
firstOrCreate
method to theHasManyThrough
relationcreateOrFirst
method to theHasManyThrough
relationIn #48529 we found a bug in the
updateOrCreate
. The issue was because that method was changed to use thefirstOrCreate
behind the scenes. Since thefirstOrCreate
method didn't exist in the relation, the method call was forwarded to the query builder, so the query changed fromselect users.* from users ... inner join teams ...
toselect * from users ... inner join teams ...
, which resulted in a bug that was causing updates on the wrong model when usingupdateOrCreate
(I shared more context here).The
updateOrCreate
was reverted to the previous implementation. However, this bug will still happen if someone calls the->firstOrCreate()
or the->createOrFirst()
methods on aHasManyThrough
relation (for the same reason, the method calls would be forwarded to the query builder and it would run aselect * from users ... inner join teams ...
causing the result to have 2id
columns, which caused the original bug).We're adding the missing
firstOrCreate
andcreateOrFirst
methods to fix the issue on theHasManyThrough
relation. I was also able to reproduce the original regression error on both methods.