Skip to content

Commit 7adad40

Browse files
[12.x] Add whereJsonContainsKey and whereJsonDoesntContainKey methods (#10573)
* Add whereJsonContainsKey and whereJsonDoesntContainKey methods. * Update queries.md --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9285ebd commit 7adad40

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

queries.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,23 +689,43 @@ $users = DB::table('users')
689689
->get();
690690
```
691691

692-
You may use `whereJsonContains` to query JSON arrays:
692+
You may use the `whereJsonContains` and `whereJsonDoesntContain` methods to query JSON arrays:
693693

694694
```php
695695
$users = DB::table('users')
696696
->whereJsonContains('options->languages', 'en')
697697
->get();
698+
699+
$users = DB::table('users')
700+
->whereJsonDoesntContain('options->languages', 'en')
701+
->get();
698702
```
699703

700-
If your application uses the MariaDB, MySQL, or PostgreSQL databases, you may pass an array of values to the `whereJsonContains` method:
704+
If your application uses the MariaDB, MySQL, or PostgreSQL databases, you may pass an array of values to the `whereJsonContains` and `whereJsonDoesntContain` methods:
701705

702706
```php
703707
$users = DB::table('users')
704708
->whereJsonContains('options->languages', ['en', 'de'])
705709
->get();
710+
711+
$users = DB::table('users')
712+
->whereJsonDoesntContain('options->languages', ['en', 'de'])
713+
->get();
714+
```
715+
716+
In addition, you may use the `whereJsonContainsKey` or `whereJsonDoesntContainKey` methods to retrieve the results that include or do not include a JSON key:
717+
718+
```php
719+
$users = DB::table('users')
720+
->whereJsonContainsKey('preferences->dietary_requirements')
721+
->get();
722+
723+
$users = DB::table('users')
724+
->whereJsonDoesntContainKey('preferences->dietary_requirements')
725+
->get();
706726
```
707727

708-
You may use `whereJsonLength` method to query JSON arrays by their length:
728+
Finally, you may use `whereJsonLength` method to query JSON arrays by their length:
709729

710730
```php
711731
$users = DB::table('users')

0 commit comments

Comments
 (0)