File tree Expand file tree Collapse file tree 4 files changed +27
-2
lines changed Expand file tree Collapse file tree 4 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44## [ 4.4.0] - unreleased
55
66* Support collection name prefix by @GromNaN in [ #2930 ] ( https://github.com/mongodb/laravel-mongodb/pull/2930 )
7+ * Ignore ` _id: null ` to let MongoDB generate an ` ObjectId ` by @GromNaN in [ #2969 ] ( https://github.com/mongodb/laravel-mongodb/pull/2969 )
78* Add ` mongodb ` driver for Batching by @GromNaN in [ #2904 ] ( https://github.com/mongodb/laravel-mongodb/pull/2904 )
89* Rename queue option ` table ` to ` collection `
910* Replace queue option ` expire ` with ` retry_after `
Original file line number Diff line number Diff line change 2121use function collect ;
2222use function is_array ;
2323use function iterator_to_array ;
24+ use function json_encode ;
2425
2526/** @method \MongoDB\Laravel\Query\Builder toBase() */
2627class Builder extends EloquentBuilder
@@ -210,8 +211,8 @@ public function raw($value = null)
210211 */
211212 public function createOrFirst (array $ attributes = [], array $ values = []): Model
212213 {
213- if ($ attributes === []) {
214- throw new InvalidArgumentException ('You must provide attributes to check for duplicates ' );
214+ if ($ attributes === [] || $ attributes === [ ' _id ' => null ] ) {
215+ throw new InvalidArgumentException ('You must provide attributes to check for duplicates. Got ' . json_encode ( $ attributes ) );
215216 }
216217
217218 // Apply casting and default values to the attributes
Original file line number Diff line number Diff line change @@ -745,6 +745,12 @@ protected function isBSON(mixed $value): bool
745745 */
746746 public function save (array $ options = [])
747747 {
748+ // SQL databases would use autoincrement the id field if set to null.
749+ // Apply the same behavior to MongoDB with _id only, otherwise null would be stored.
750+ if (array_key_exists ('_id ' , $ this ->attributes ) && $ this ->attributes ['_id ' ] === null ) {
751+ unset($ this ->attributes ['_id ' ]);
752+ }
753+
748754 $ saved = parent ::save ($ options );
749755
750756 // Clear list of unset fields
Original file line number Diff line number Diff line change @@ -1151,4 +1151,21 @@ public function testUpdateOrCreate(array $criteria)
11511151 $ this ->assertEquals ($ createdAt , $ checkUser ->created_at ->getTimestamp ());
11521152 $ this ->assertEquals ($ updatedAt , $ checkUser ->updated_at ->getTimestamp ());
11531153 }
1154+
1155+ public function testCreateWithNullId ()
1156+ {
1157+ $ user = User::create (['_id ' => null , 'email ' => 'foo@bar ' ]);
1158+ $ this ->assertNotNull (ObjectId::class, $ user ->id );
1159+ $ this ->assertSame (1 , User::count ());
1160+ }
1161+
1162+ public function testUpdateOrCreateWithNullId ()
1163+ {
1164+ $ this ->expectException (InvalidArgumentException::class);
1165+ $ this ->expectExceptionMessage ('You must provide attributes to check for duplicates ' );
1166+ User::updateOrCreate (
1167+ ['_id ' => null ],
1168+ ['email ' => 'jane.doe@example.com ' ],
1169+ );
1170+ }
11541171}
You can’t perform that action at this time.
0 commit comments