Skip to content

Update push and pull docs #2685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 4, 2023
71 changes: 53 additions & 18 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,17 @@ Add one or multiple values to the items array.
// Push the value to the matched documents
DB::collection('users')
->where('name', 'John')
// Push a single value to the items array
->push('items', 'boots'); // items: ['boots']
// Push a single value to the items array
->push('items', 'boots');
// Result:
// items: ['boots']

DB::collection('users')
->where('name', 'John')
// Push multiple values to the items array
->push('items', ['hat', 'jeans']); // items: ['boots', 'hat', 'jeans']
// Push multiple values to the items array
->push('items', ['hat', 'jeans']);
// Result:
// items: ['boots', 'hat', 'jeans']

// Or

Expand All @@ -469,7 +473,8 @@ $user->push('items', 'boots');
$user->push('items', ['hat', 'jeans']);
```

To add an array to the messages array.
To add an array as a value to the messages array. value arrays must be nested into a list array, otherwise they are
interpreted as a list of scalar values.

```php
DB::collection('users')
Expand All @@ -480,7 +485,11 @@ DB::collection('users')
'from' => 'Jane Doe',
'message' => 'Hi John',
],
]); // messages: [ ['from' => 'Jane Doe', 'message' => 'Hi John'] ]
]);
// Result:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// ]

// Or

Expand All @@ -497,11 +506,15 @@ If you **DON'T** want duplicate values, set the third parameter to `true`:
```php
DB::collection('users')
->where('name', 'John')
->push('items', 'boots'); // items: ['boots']
->push('items', 'boots');
// Result:
// items: ['boots']

DB::collection('users')
->where('name', 'John')
->push('items', ['hat', 'boots', 'jeans'], true); // items: ['boots', 'hat', 'jeans']
->push('items', ['hat', 'boots', 'jeans'], true);
// Result:
// items: ['boots', 'hat', 'jeans']

// Or

Expand All @@ -510,18 +523,27 @@ $user->push('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
],
]); // messages: [ ['from' => 'Jane Doe', 'message' => 'Hi John'] ]
]);
// Result:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// ]

$user->push('messages', [
[
'from' => 'Jess Doe',
'message' => 'Hi John',
'message' => 'Hi',
],
[
'from' => 'Jane Doe',
'message' => 'Hi John',
],
], true); // messages: [ ['from' => 'Jane Doe', 'message' => 'Hi John'], ['from' => 'Jess Doe', 'message' => 'Hi John'] ]
], true);
// Result:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// { 'from' => 'Jess Doe', 'message' => 'Hi' }
// ]
```

**Pull**
Expand All @@ -533,18 +555,25 @@ Remove one or multiple values form items array.

DB::collection('users')
->where('name', 'John')
// Pull a single value
->pull('items', 'boots'); // items: ['hat', 'jeans']
->pull('items', 'boots'); // Pull a single value
// Result:
// items: ['hat', 'jeans']

// Or pull multiple values

$user->pull('items', ['boots', 'jeans']); // items: ['hat']
$user->pull('items', ['boots', 'jeans']);
// Result:
// items: ['hat']
```

Also, you can pull an array value from the messages array.

```php
// messages: [ ['from' => 'Jane Doe', 'message' => 'Hi John'], ['from' => 'Jess Doe', 'message' => 'Hi John'] ]
// Latest state:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// { 'from' => 'Jess Doe', 'message' => 'Hi' }
// ]

DB::collection('users')
->where('name', 'John')
Expand All @@ -554,7 +583,11 @@ DB::collection('users')
'from' => 'Jane Doe',
'message' => 'Hi John',
]
]); // messages: [ ['from' => 'Jess Doe', 'message' => 'Hi John'] ]
]);
// Result:
// messages: [
// { 'from' => 'Jess Doe', 'message' => 'Hi' }
// ]

// Or pull multiple array

Expand All @@ -565,9 +598,11 @@ $user->pull('messages', [
],
[
'from' => 'Jess Doe',
'message' => 'Hi John',
'message' => 'Hi',
]
]); // messages: [ ]
]);
// Result:
// messages: [ ]
```

**Unset**
Expand Down