Skip to content

Commit 286eebf

Browse files
committed
Fix findOneAndUpdate options
1 parent 206745b commit 286eebf

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Eloquent/Builder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MongoDB\Laravel\Helpers\QueriesRelationships;
1313
use MongoDB\Laravel\Internal\FindAndModifyCommandSubscriber;
1414
use MongoDB\Model\BSONDocument;
15+
use MongoDB\Operation\FindOneAndUpdate;
1516

1617
use function array_intersect_key;
1718
use function array_key_exists;
@@ -201,6 +202,7 @@ public function createOrFirst(array $attributes = [], array $values = []): Model
201202
}
202203

203204
// Apply casting and default values to the attributes
205+
// In case of duplicate key between the attributes and the values, the values have priority
204206
$instance = $this->newModelInstance($values + $attributes);
205207
$values = $instance->getAttributes();
206208
$attributes = array_intersect_key($attributes, $values);
@@ -212,8 +214,12 @@ public function createOrFirst(array $attributes = [], array $values = []): Model
212214
try {
213215
$document = $collection->findOneAndUpdate(
214216
$attributes,
215-
['$setOnInsert' => $values],
216-
['upsert' => true, 'new' => true, 'typeMap' => ['root' => 'array', 'document' => 'array']],
217+
['$setOnInsert' => (object) $values],
218+
[
219+
'upsert' => true,
220+
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
221+
'typeMap' => ['root' => 'array', 'document' => 'array'],
222+
],
217223
);
218224
} finally {
219225
$collection->getManager()->removeSubscriber($listener);

0 commit comments

Comments
 (0)