@@ -448,64 +448,136 @@ DB::collection('items')
448
448
449
449
** Push**
450
450
451
- Add items to an array.
451
+ Add one or multiple values to the ` items ` array.
452
452
453
453
``` php
454
+ // Push the value to the matched documents
454
455
DB::collection('users')
455
- ->where('name', 'John')
456
- ->push('items', 'boots');
456
+ ->where('name', 'John')
457
+ // Push a single value to the items array
458
+ ->push('items', 'boots');
459
+ // Result:
460
+ // items: ['boots']
461
+
462
+ DB::collection('users')
463
+ ->where('name', 'John')
464
+ // Push multiple values to the items array
465
+ ->push('items', ['hat', 'jeans']);
466
+ // Result:
467
+ // items: ['boots', 'hat', 'jeans']
457
468
469
+ // Or
470
+
471
+ // Push the values directly to a model object
458
472
$user->push('items', 'boots');
473
+ $user->push('items', ['hat', 'jeans']);
459
474
```
460
475
476
+ To add embedded document or array values to the ` messages ` array, those values must be specified within a list array.
477
+
461
478
``` php
462
479
DB::collection('users')
463
- ->where('name', 'John')
464
- ->push('messages', [
465
- 'from' => 'Jane Doe',
466
- 'message' => 'Hi John',
467
- ]);
480
+ ->where('name', 'John')
481
+ // Push an embedded document as a value to the messages array
482
+ ->push('messages', [
483
+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
484
+ ]);
485
+ // Result:
486
+ // messages: [
487
+ // { from: "Jane Doe", message: "Hi John" }
488
+ // ]
489
+
490
+ // Or
468
491
469
492
$user->push('messages', [
470
- 'from' => 'Jane Doe',
471
- 'message' => 'Hi John',
493
+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
472
494
]);
473
495
```
474
496
475
- If you ** DON'T** want duplicate items , set the third parameter to ` true ` :
497
+ If you ** DON'T** want duplicate values , set the third parameter to ` true ` :
476
498
477
499
``` php
478
500
DB::collection('users')
479
- ->where('name', 'John')
480
- ->push('items', 'boots', true);
501
+ ->where('name', 'John')
502
+ ->push('items', 'boots');
503
+ // Result:
504
+ // items: ['boots']
505
+
506
+ DB::collection('users')
507
+ ->where('name', 'John')
508
+ ->push('items', ['hat', 'boots', 'jeans'], true);
509
+ // Result:
510
+ // items: ['boots', 'hat', 'jeans']
511
+
512
+ // Or
481
513
482
- $user->push('items', 'boots', true);
514
+ $user->push('messages', [
515
+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
516
+ ]);
517
+ // Result:
518
+ // messages: [
519
+ // { from: "Jane Doe", message: "Hi John" }
520
+ // ]
521
+
522
+ $user->push('messages', [
523
+ [ 'from' => 'Jess Doe', 'message' => 'Hi' ],
524
+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ],
525
+ ], true);
526
+ // Result:
527
+ // messages: [
528
+ // { from: "Jane Doe", message: "Hi John" }
529
+ // { from: "Jess Doe", message: "Hi" }
530
+ // ]
483
531
```
484
532
485
533
** Pull**
486
534
487
- Remove an item from an array.
535
+ Remove one or multiple values from the ` items ` array.
488
536
489
537
``` php
538
+ // items: ['boots', 'hat', 'jeans']
539
+
490
540
DB::collection('users')
491
- ->where('name', 'John')
492
- ->pull('items', 'boots');
541
+ ->where('name', 'John')
542
+ ->pull('items', 'boots'); // Pull a single value
543
+ // Result:
544
+ // items: ['hat', 'jeans']
493
545
494
- $user->pull('items', 'boots');
546
+ // Or pull multiple values
547
+
548
+ $user->pull('items', ['boots', 'jeans']);
549
+ // Result:
550
+ // items: ['hat']
495
551
```
496
552
553
+ Embedded document and arrays values can also be removed from the ` messages ` array.
554
+
497
555
``` php
556
+ // Latest state:
557
+ // messages: [
558
+ // { from: "Jane Doe", message: "Hi John" }
559
+ // { from: "Jess Doe", message: "Hi" }
560
+ // ]
561
+
498
562
DB::collection('users')
499
- ->where('name', 'John')
500
- ->pull('messages', [
501
- 'from' => 'Jane Doe',
502
- 'message' => 'Hi John',
503
- ]);
563
+ ->where('name', 'John')
564
+ // Pull an embedded document from the array
565
+ ->pull('messages', [
566
+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
567
+ ]);
568
+ // Result:
569
+ // messages: [
570
+ // { from: "Jess Doe", message: "Hi" }
571
+ // ]
572
+
573
+ // Or pull multiple embedded documents
504
574
505
575
$user->pull('messages', [
506
- 'from' => 'Jane Doe',
507
- 'message ' => 'Hi John',
576
+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ] ,
577
+ [ 'from ' => 'Jess Doe', 'message' => 'Hi' ]
508
578
]);
579
+ // Result:
580
+ // messages: [ ]
509
581
```
510
582
511
583
** Unset**
0 commit comments