Skip to content

Commit

Permalink
Enhancing updateOrCreate() to Use firstOrCreate() (#48160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw authored Aug 27, 2023
1 parent 24cbb80 commit 80e0262
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,10 @@ public function createOrFirst(array $attributes = [], array $values = [])
*/
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values)->save();
return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) {
if (! $instance->wasRecentlyCreated) {
$instance->fill($values)->save();
}
});
}

Expand Down

0 comments on commit 80e0262

Please sign in to comment.