diff --git a/CHANGELOG.md b/CHANGELOG.md index b001519ae..c2b4dddca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,17 @@ All notable changes to this project will be documented in this file. * **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040) -## [4.8.0] - next +## [4.8.0] - 2024-08-27 * Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550) * Add `Query\Builder::whereLike()` and `whereNotLike()` methods by @GromNaN in [#3108](https://github.com/mongodb/laravel-mongodb/pull/3108) * Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062) * Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064) +## [4.7.2] - 2024-08-27 + +* Add `Query\Builder::upsert()` method with a single document by @GromNaN in [#3100](https://github.com/mongodb/laravel-mongodb/pull/3100) + ## [4.7.1] - 2024-07-25 * Fix registration of `BusServiceProvider` for compatibility with Horizon by @GromNaN in [#3071](https://github.com/mongodb/laravel-mongodb/pull/3071) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index cb23b8677..b41168b80 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -746,6 +746,11 @@ public function upsert(array $values, $uniqueBy, $update = null): int return 0; } + // Single document provided + if (! array_is_list($values)) { + $values = [$values]; + } + $this->applyBeforeQueryCallbacks(); $options = $this->inheritConnectionOptions(); diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 6903ce64c..36465ce53 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -168,9 +168,8 @@ public function testUpsert() $this->assertSame('bar2', User::where('email', 'foo')->first()->name); // If no update fields are specified, all fields are updated - $result = User::upsert([ - ['email' => 'foo', 'name' => 'bar3'], - ], 'email'); + // Test single document update + $result = User::upsert(['email' => 'foo', 'name' => 'bar3'], 'email'); $this->assertSame(1, $result); $this->assertSame(2, User::count()); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 0495f38aa..436c86996 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -633,9 +633,8 @@ public function testUpsert() $this->assertSame('bar2', DB::table('users')->where('email', 'foo')->first()['name']); // If no update fields are specified, all fields are updated - $result = DB::table('users')->upsert([ - ['email' => 'foo', 'name' => 'bar3'], - ], 'email'); + // Test single document update + $result = DB::table('users')->upsert(['email' => 'foo', 'name' => 'bar3'], 'email'); $this->assertSame(1, $result); $this->assertSame(2, DB::table('users')->count());