@@ -375,7 +375,7 @@ public function toMql(): array
375
375
376
376
// Apply order and limit
377
377
if ($ this ->orders ) {
378
- $ pipeline [] = ['$sort ' => $ this ->orders ];
378
+ $ pipeline [] = ['$sort ' => $ this ->aliasIdForQuery ( $ this -> orders ) ];
379
379
}
380
380
381
381
if ($ this ->offset ) {
@@ -416,7 +416,7 @@ public function toMql(): array
416
416
417
417
// Normal query
418
418
// Convert select columns to simple projections.
419
- $ projection = array_fill_keys ($ columns , true );
419
+ $ projection = $ this -> aliasIdForQuery ( array_fill_keys ($ columns , true ) );
420
420
421
421
// Add custom projections.
422
422
if ($ this ->projections ) {
@@ -431,7 +431,7 @@ public function toMql(): array
431
431
}
432
432
433
433
if ($ this ->orders ) {
434
- $ options ['sort ' ] = $ this ->orders ;
434
+ $ options ['sort ' ] = $ this ->aliasIdForQuery ( $ this -> orders ) ;
435
435
}
436
436
437
437
if ($ this ->offset ) {
@@ -506,7 +506,7 @@ public function getFresh($columns = [], $returnLazy = false)
506
506
if ($ returnLazy ) {
507
507
return LazyCollection::make (function () use ($ result ) {
508
508
foreach ($ result as $ item ) {
509
- yield $ item ;
509
+ yield $ this -> aliasIdForResult ( $ item) ;
510
510
}
511
511
});
512
512
}
@@ -515,6 +515,10 @@ public function getFresh($columns = [], $returnLazy = false)
515
515
$ result = $ result ->toArray ();
516
516
}
517
517
518
+ foreach ($ result as &$ document ) {
519
+ $ document = $ this ->aliasIdForResult ($ document );
520
+ }
521
+
518
522
return new Collection ($ result );
519
523
}
520
524
@@ -593,7 +597,7 @@ public function aggregate($function = null, $columns = ['*'])
593
597
/** @inheritdoc */
594
598
public function exists ()
595
599
{
596
- return $ this ->first (['_id ' ]) !== null ;
600
+ return $ this ->first (['id ' ]) !== null ;
597
601
}
598
602
599
603
/** @inheritdoc */
@@ -682,6 +686,18 @@ public function insert(array $values)
682
686
$ values = [$ values ];
683
687
}
684
688
689
+ // Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
690
+ foreach ($ values as &$ document ) {
691
+ if (isset ($ document ['id ' ])) {
692
+ if (isset ($ document ['_id ' ]) && $ document ['_id ' ] !== $ document ['id ' ]) {
693
+ throw new InvalidArgumentException ('Cannot insert document with different "id" and "_id" values ' );
694
+ }
695
+
696
+ $ document ['_id ' ] = $ document ['id ' ];
697
+ unset($ document ['id ' ]);
698
+ }
699
+ }
700
+
685
701
$ options = $ this ->inheritConnectionOptions ();
686
702
687
703
$ result = $ this ->collection ->insertMany ($ values , $ options );
@@ -700,11 +716,10 @@ public function insertGetId(array $values, $sequence = null)
700
716
return null ;
701
717
}
702
718
703
- if ($ sequence === null || $ sequence === '_id ' ) {
704
- return $ result ->getInsertedId ();
705
- }
706
-
707
- return $ values [$ sequence ];
719
+ return match ($ sequence ) {
720
+ '_id ' , 'id ' , null => $ result ->getInsertedId (),
721
+ default => $ values [$ sequence ],
722
+ };
708
723
}
709
724
710
725
/** @inheritdoc */
@@ -720,7 +735,12 @@ public function update(array $values, array $options = [])
720
735
unset($ values [$ key ]);
721
736
}
722
737
723
- $ 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
+ }
724
744
725
745
return $ this ->performUpdate ($ values , $ options );
726
746
}
@@ -821,18 +841,6 @@ public function decrementEach(array $columns, array $extra = [], array $options
821
841
return $ this ->incrementEach ($ decrement , $ extra , $ options );
822
842
}
823
843
824
- /** @inheritdoc */
825
- public function chunkById ($ count , callable $ callback , $ column = '_id ' , $ alias = null )
826
- {
827
- return parent ::chunkById ($ count , $ callback , $ column , $ alias );
828
- }
829
-
830
- /** @inheritdoc */
831
- public function forPageAfterId ($ perPage = 15 , $ lastId = 0 , $ column = '_id ' )
832
- {
833
- return parent ::forPageAfterId ($ perPage , $ lastId , $ column );
834
- }
835
-
836
844
/** @inheritdoc */
837
845
public function pluck ($ column , $ key = null )
838
846
{
@@ -1048,21 +1056,26 @@ public function runPaginationCountQuery($columns = ['*'])
1048
1056
/**
1049
1057
* Perform an update query.
1050
1058
*
1051
- * @param array $query
1052
- *
1053
1059
* @return int
1054
1060
*/
1055
- protected function performUpdate ($ query , array $ options = [])
1061
+ protected function performUpdate (array $ update , array $ options = [])
1056
1062
{
1057
1063
// Update multiple items by default.
1058
1064
if (! array_key_exists ('multiple ' , $ options )) {
1059
1065
$ options ['multiple ' ] = true ;
1060
1066
}
1061
1067
1068
+ // Since "id" is an alias for "_id", we prevent updating it
1069
+ foreach ($ update as $ operator => $ fields ) {
1070
+ if (array_key_exists ('id ' , $ fields )) {
1071
+ throw new InvalidArgumentException ('Cannot update "id" field. ' );
1072
+ }
1073
+ }
1074
+
1062
1075
$ options = $ this ->inheritConnectionOptions ($ options );
1063
1076
1064
1077
$ wheres = $ this ->compileWheres ();
1065
- $ result = $ this ->collection ->updateMany ($ wheres , $ query , $ options );
1078
+ $ result = $ this ->collection ->updateMany ($ wheres , $ update , $ options );
1066
1079
if ($ result ->isAcknowledged ()) {
1067
1080
return $ result ->getModifiedCount () ? $ result ->getModifiedCount () : $ result ->getUpsertedCount ();
1068
1081
}
@@ -1155,16 +1168,21 @@ protected function compileWheres(): array
1155
1168
// Convert column name to string to use as array key
1156
1169
if (isset ($ where ['column ' ])) {
1157
1170
$ where ['column ' ] = (string ) $ where ['column ' ];
1158
- }
1159
1171
1160
- // Convert id's.
1161
- if (isset ($ where ['column ' ]) && ($ where ['column ' ] === '_id ' || str_ends_with ($ where ['column ' ], '._id ' ))) {
1162
- if (isset ($ where ['values ' ])) {
1163
- // Multiple values.
1164
- $ where ['values ' ] = array_map ($ this ->convertKey (...), $ where ['values ' ]);
1165
- } elseif (isset ($ where ['value ' ])) {
1166
- // Single value.
1167
- $ where ['value ' ] = $ this ->convertKey ($ where ['value ' ]);
1172
+ // Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
1173
+ if ($ where ['column ' ] === 'id ' ) {
1174
+ $ where ['column ' ] = '_id ' ;
1175
+ }
1176
+
1177
+ // Convert id's.
1178
+ if ($ where ['column ' ] === '_id ' || str_ends_with ($ where ['column ' ], '._id ' )) {
1179
+ if (isset ($ where ['values ' ])) {
1180
+ // Multiple values.
1181
+ $ where ['values ' ] = array_map ($ this ->convertKey (...), $ where ['values ' ]);
1182
+ } elseif (isset ($ where ['value ' ])) {
1183
+ // Single value.
1184
+ $ where ['value ' ] = $ this ->convertKey ($ where ['value ' ]);
1185
+ }
1168
1186
}
1169
1187
}
1170
1188
@@ -1604,4 +1622,23 @@ public function orWhereIntegerNotInRaw($column, $values, $boolean = 'and')
1604
1622
{
1605
1623
throw new BadMethodCallException ('This method is not supported by MongoDB ' );
1606
1624
}
1625
+
1626
+ private function aliasIdForQuery (array $ values ): array
1627
+ {
1628
+ if (isset ($ values ['id ' ])) {
1629
+ $ values ['_id ' ] = $ values ['id ' ];
1630
+ unset($ values ['id ' ]);
1631
+ }
1632
+
1633
+ return $ values ;
1634
+ }
1635
+
1636
+ private function aliasIdForResult (array $ values ): array
1637
+ {
1638
+ if (isset ($ values ['_id ' ])) {
1639
+ $ values ['id ' ] = $ values ['_id ' ];
1640
+ }
1641
+
1642
+ return $ values ;
1643
+ }
1607
1644
}
0 commit comments