@@ -448,64 +448,136 @@ DB::collection('items')
448448
449449** Push**
450450
451- Add items to an array.
451+ Add one or multiple values to the ` items ` array.
452452
453453``` php
454+ // Push the value to the matched documents
454455DB::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']
457468
469+ // Or
470+
471+ // Push the values directly to a model object
458472$user->push('items', 'boots');
473+ $user->push('items', ['hat', 'jeans']);
459474```
460475
476+ To add embedded document or array values to the ` messages ` array, those values must be specified within a list array.
477+
461478``` php
462479DB::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
468491
469492$user->push('messages', [
470- 'from' => 'Jane Doe',
471- 'message' => 'Hi John',
493+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
472494]);
473495```
474496
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 ` :
476498
477499``` php
478500DB::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
481513
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+ // ]
483531```
484532
485533** Pull**
486534
487- Remove an item from an array.
535+ Remove one or multiple values from the ` items ` array.
488536
489537``` php
538+ // items: ['boots', 'hat', 'jeans']
539+
490540DB::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']
493545
494- $user->pull('items', 'boots');
546+ // Or pull multiple values
547+
548+ $user->pull('items', ['boots', 'jeans']);
549+ // Result:
550+ // items: ['hat']
495551```
496552
553+ Embedded document and arrays values can also be removed from the ` messages ` array.
554+
497555``` php
556+ // Latest state:
557+ // messages: [
558+ // { from: "Jane Doe", message: "Hi John" }
559+ // { from: "Jess Doe", message: "Hi" }
560+ // ]
561+
498562DB::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
504574
505575$user->pull('messages', [
506- 'from' => 'Jane Doe',
507- 'message ' => 'Hi John',
576+ [ 'from' => 'Jane Doe', 'message' => 'Hi John' ] ,
577+ [ 'from ' => 'Jess Doe', 'message' => 'Hi' ]
508578]);
579+ // Result:
580+ // messages: [ ]
509581```
510582
511583** Unset**
0 commit comments