From c3c7c08e6f097640f97f1bf2bdb9947d07298389 Mon Sep 17 00:00:00 2001 From: mpyw Date: Thu, 24 Aug 2023 14:56:14 +0900 Subject: [PATCH] Enhancing `updateOrCreate()` to Use `firstOrCreate()` --- 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 2e17922c5353..59cc788eb095 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(); + } }); }