-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 9 commits
18701c9
36323f3
7fd5a6a
19330c5
9ba44df
d47380c
ac3d9ab
010663f
c2bd491
7cb4def
982d724
509b009
a55fc86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -448,64 +448,161 @@ 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 value to an exact document | ||
$user->push('items', 'boots'); | ||
$user->push('items', ['hat', 'jeans']); | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
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. | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```php | ||
DB::collection('users') | ||
->where('name', 'John') | ||
->push('messages', [ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
]); | ||
->where('name', 'John') | ||
// Push an array as a value to the items array | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->push('messages', [ | ||
[ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
], | ||
]); | ||
// Result: | ||
// messages: [ | ||
// { 'from' => 'Jane Doe', 'message' => 'Hi John' } | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ] | ||
|
||
// 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`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't checked the code, but this suggests that the boolean causes Having a boolean argument control that rubs me the wrong way (I'll add it to the growing list), but I assume there's an explanation for this behavior. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. It's the logged queries of pushing normally and pushing unique values.
The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is Eloquent's @GromNaN: A boolean flag to determine whether It may be worth making a ticket to revisit this down the line. Perhaps introduce a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 agree that the double behavior of this method is a bad design. If deprecate the custom behavior of Let's improve the documentation for now. |
||
|
||
```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('messages', [ | ||
[ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
], | ||
]); | ||
// Result: | ||
// messages: [ | ||
// { 'from' => 'Jane Doe', 'message' => 'Hi John' } | ||
// ] | ||
|
||
$user->push('items', 'boots', true); | ||
$user->push('messages', [ | ||
[ | ||
'from' => 'Jess Doe', | ||
'message' => 'Hi', | ||
], | ||
[ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
], | ||
], true); | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 form items array. | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```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'] | ||
|
||
// Or pull multiple values | ||
|
||
$user->pull('items', 'boots'); | ||
$user->pull('items', ['boots', 'jeans']); | ||
// Result: | ||
// items: ['hat'] | ||
``` | ||
|
||
Also, you can pull an array value from the messages array. | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```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') | ||
// It will search for the exact array and pulls it | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->pull('messages', [ | ||
[ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
] | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]); | ||
// Result: | ||
// messages: [ | ||
// { 'from' => 'Jess Doe', 'message' => 'Hi' } | ||
// ] | ||
|
||
// Or pull multiple array | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$user->pull('messages', [ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
[ | ||
'from' => 'Jane Doe', | ||
'message' => 'Hi John', | ||
], | ||
[ | ||
'from' => 'Jess Doe', | ||
'message' => 'Hi', | ||
] | ||
]); | ||
// Result: | ||
// messages: [ ] | ||
``` | ||
|
||
**Unset** | ||
|
Uh oh!
There was an error while loading. Please reload this page.