Skip to content

Commit

Permalink
Update push and pull docs (#2685)
Browse files Browse the repository at this point in the history
Co-Authored-By: Jeremy Mikola <jmikola@gmail.com>
  • Loading branch information
hans-thomas and jmikola committed Dec 4, 2023
1 parent fc1f9cc commit 24c3592
Showing 1 changed file with 97 additions and 25 deletions.
122 changes: 97 additions & 25 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,64 +448,136 @@ DB::collection('items')

**Push**

Add items to an array.
Add one or multiple values to the `items` array.

```php
// Push the value to the matched documents
DB::collection('users')
->where('name', 'John')
->push('items', 'boots');
->where('name', 'John')
// 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']);
// Result:
// items: ['boots', 'hat', 'jeans']

// Or

// Push the values directly to a model object
$user->push('items', 'boots');
$user->push('items', ['hat', 'jeans']);
```

To add embedded document or array values to the `messages` array, those values must be specified within a list array.

```php
DB::collection('users')
->where('name', 'John')
->push('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
]);
->where('name', 'John')
// Push an embedded document as a value to the messages array
->push('messages', [
[ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
]);
// Result:
// messages: [
// { from: "Jane Doe", message: "Hi John" }
// ]

// Or

$user->push('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
[ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
]);
```

If you **DON'T** want duplicate items, set the third parameter to `true`:
If you **DON'T** want duplicate values, set the third parameter to `true`:

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

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

// Or

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

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

**Pull**

Remove an item from an array.
Remove one or multiple values from the `items` array.

```php
// items: ['boots', 'hat', 'jeans']

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

$user->pull('items', 'boots');
// Or pull multiple values

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

Embedded document and arrays values can also be removed from the `messages` array.

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

DB::collection('users')
->where('name', 'John')
->pull('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
]);
->where('name', 'John')
// Pull an embedded document from the array
->pull('messages', [
[ 'from' => 'Jane Doe', 'message' => 'Hi John' ]
]);
// Result:
// messages: [
// { from: "Jess Doe", message: "Hi" }
// ]

// Or pull multiple embedded documents

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

**Unset**
Expand Down

0 comments on commit 24c3592

Please sign in to comment.