@@ -375,7 +375,7 @@ public function toMql(): array
375375
376376 // Apply order and limit
377377 if ($ this ->orders ) {
378- $ pipeline [] = ['$sort ' => $ this ->renameId ($ this ->orders )];
378+ $ pipeline [] = ['$sort ' => $ this ->aliasIdForQuery ($ this ->orders )];
379379 }
380380
381381 if ($ this ->offset ) {
@@ -416,7 +416,7 @@ public function toMql(): array
416416
417417 // Normal query
418418 // Convert select columns to simple projections.
419- $ projection = $ this ->renameId (array_fill_keys ($ columns , true ));
419+ $ projection = $ this ->aliasIdForQuery (array_fill_keys ($ columns , true ));
420420
421421 // Add custom projections.
422422 if ($ this ->projections ) {
@@ -431,7 +431,7 @@ public function toMql(): array
431431 }
432432
433433 if ($ this ->orders ) {
434- $ options ['sort ' ] = $ this ->renameId ($ this ->orders );
434+ $ options ['sort ' ] = $ this ->aliasIdForQuery ($ this ->orders );
435435 }
436436
437437 if ($ this ->offset ) {
@@ -506,7 +506,7 @@ public function getFresh($columns = [], $returnLazy = false)
506506 if ($ returnLazy ) {
507507 return LazyCollection::make (function () use ($ result ) {
508508 foreach ($ result as $ item ) {
509- yield $ item ;
509+ yield $ this -> aliasIdForResult ( $ item) ;
510510 }
511511 });
512512 }
@@ -515,6 +515,10 @@ public function getFresh($columns = [], $returnLazy = false)
515515 $ result = $ result ->toArray ();
516516 }
517517
518+ foreach ($ result as &$ document ) {
519+ $ document = $ this ->aliasIdForResult ($ document );
520+ }
521+
518522 return new Collection ($ result );
519523 }
520524
@@ -593,7 +597,7 @@ public function aggregate($function = null, $columns = ['*'])
593597 /** @inheritdoc */
594598 public function exists ()
595599 {
596- return $ this ->first (['_id ' ]) !== null ;
600+ return $ this ->first (['id ' ]) !== null ;
597601 }
598602
599603 /** @inheritdoc */
@@ -690,6 +694,7 @@ public function insert(array $values)
690694 }
691695
692696 $ document ['_id ' ] = $ document ['id ' ];
697+ unset($ document ['id ' ]);
693698 }
694699 }
695700
@@ -711,11 +716,10 @@ public function insertGetId(array $values, $sequence = null)
711716 return null ;
712717 }
713718
714- if ($ sequence === null || $ sequence === '_id ' ) {
715- return $ result ->getInsertedId ();
716- }
717-
718- return $ values [$ sequence ];
719+ return match ($ sequence ) {
720+ '_id ' , 'id ' , null => $ result ->getInsertedId (),
721+ default => $ values [$ sequence ],
722+ };
719723 }
720724
721725 /** @inheritdoc */
@@ -731,7 +735,12 @@ public function update(array $values, array $options = [])
731735 unset($ values [$ key ]);
732736 }
733737
734- $ options = $ this ->inheritConnectionOptions ($ options );
738+ // Since "id" is an alias for "_id", we prevent updating it
739+ foreach ($ values as $ fields ) {
740+ if (array_key_exists ('id ' , $ fields )) {
741+ throw new InvalidArgumentException ('Cannot update "id" field. ' );
742+ }
743+ }
735744
736745 return $ this ->performUpdate ($ values , $ options );
737746 }
@@ -978,21 +987,26 @@ public function runPaginationCountQuery($columns = ['*'])
978987 /**
979988 * Perform an update query.
980989 *
981- * @param array $query
982- *
983990 * @return int
984991 */
985- protected function performUpdate ($ query , array $ options = [])
992+ protected function performUpdate (array $ update , array $ options = [])
986993 {
987994 // Update multiple items by default.
988995 if (! array_key_exists ('multiple ' , $ options )) {
989996 $ options ['multiple ' ] = true ;
990997 }
991998
999+ // Since "id" is an alias for "_id", we prevent updating it
1000+ foreach ($ update as $ operator => $ fields ) {
1001+ if (array_key_exists ('id ' , $ fields )) {
1002+ throw new InvalidArgumentException ('Cannot update "id" field. ' );
1003+ }
1004+ }
1005+
9921006 $ options = $ this ->inheritConnectionOptions ($ options );
9931007
9941008 $ wheres = $ this ->compileWheres ();
995- $ result = $ this ->collection ->updateMany ($ wheres , $ query , $ options );
1009+ $ result = $ this ->collection ->updateMany ($ wheres , $ update , $ options );
9961010 if ($ result ->isAcknowledged ()) {
9971011 return $ result ->getModifiedCount () ? $ result ->getModifiedCount () : $ result ->getUpsertedCount ();
9981012 }
@@ -1540,7 +1554,7 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
15401554 throw new BadMethodCallException ('This method is not supported by MongoDB ' );
15411555 }
15421556
1543- private function renameId (array $ values ): array
1557+ private function aliasIdForQuery (array $ values ): array
15441558 {
15451559 if (isset ($ values ['id ' ])) {
15461560 $ values ['_id ' ] = $ values ['id ' ];
@@ -1549,4 +1563,13 @@ private function renameId(array $values): array
15491563
15501564 return $ values ;
15511565 }
1566+
1567+ private function aliasIdForResult (array $ values ): array
1568+ {
1569+ if (isset ($ values ['_id ' ])) {
1570+ $ values ['id ' ] = $ values ['_id ' ];
1571+ }
1572+
1573+ return $ values ;
1574+ }
15521575}
0 commit comments