From 80e0262a978e27feb9250b9367860a3743feac2b Mon Sep 17 00:00:00 2001 From: mpyw Date: Mon, 28 Aug 2023 00:12:26 +0900 Subject: [PATCH] Enhancing `updateOrCreate()` to Use `firstOrCreate()` (#48160) --- src/Illuminate/Database/Eloquent/Builder.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 7e4b2725bd2b..7f598ad83489 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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(); + } }); }