Skip to content

Commit

Permalink
Enhancing updateOrCreate() to Use firstOrCreate()
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Aug 24, 2023
1 parent 38434a7 commit c3c7c08
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 c3c7c08

Please sign in to comment.