Skip to content

Conversation

@tonysm
Copy link
Contributor

@tonysm tonysm commented Aug 26, 2023

Changed

  • In [10.x] Adds a createOrFirst method to Eloquent #47973, I only switched the Builder::firstOrCreate to also use the createOrFirst behind to scenes and missed the BelongsToMany::firstOrCreate and HasOneOrMany::firstOrCreate. This PR also changes the missed methods to use firstOrCreate instead of simply trying to create related records after not finding them, also solving the race condition issue in the relations.
  • Wraps the "attach" method call on the BelongsToMany::firstOrCreate in a SAVEPOINT if needed and a try/catch to handle the unique violation there as well, making it more robust

Hat tip to @mpyw for pointing out that I missed those methods in the original implementation.

tonysm added 2 commits August 26, 2023 00:34
This should be the same but more robust, since it handles race conditions as well
This should essentially be the same as before, but more robust
@tonysm tonysm changed the title [10.x] Use createOrFirst in relations [10.x] Make the firstOrCreate methods in relations use createOrFirst behind the scenes Aug 26, 2023
@tonysm tonysm marked this pull request as ready for review August 26, 2023 19:26
@taylorotwell taylorotwell merged commit 24cbb80 into laravel:10.x Aug 27, 2023
taylorotwell pushed a commit that referenced this pull request Aug 29, 2023
…ate` behind the scenes (#48213)

* Make the `updateOrCreate` methods in relations use `firstOrCreate`

Related to #48160, #48192

* Fix tests

* Fix: correctly apply unused `$joining` and `$touch`

* Fix: `save()` should always accepts `['touch' => false]`
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