Skip to content

[12.x] Add whereJsonContainsKey and whereJsonDoesntContainKey methods #10573

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,23 +689,43 @@ $users = DB::table('users')
->get();
```

You may use `whereJsonContains` to query JSON arrays:
You may use the `whereJsonContains` and `whereJsonDoesntContain` methods to query JSON arrays:

```php
$users = DB::table('users')
->whereJsonContains('options->languages', 'en')
->get();

$users = DB::table('users')
->whereJsonDoesntContain('options->languages', 'en')
->get();
```

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

```php
$users = DB::table('users')
->whereJsonContains('options->languages', ['en', 'de'])
->get();

$users = DB::table('users')
->whereJsonDoesntContain('options->languages', ['en', 'de'])
->get();
```

In addition, you may use the `whereJsonContainsKey` or `whereJsonDoesntContainKey` methods to retrieve the results that include or do not include a JSON key:

```php
$users = DB::table('users')
->whereJsonContainsKey('preferences->dietary_requirements')
->get();

$users = DB::table('users')
->whereJsonDoesntContainKey('preferences->dietary_requirements')
->get();
```

You may use `whereJsonLength` method to query JSON arrays by their length:
Finally, you may use `whereJsonLength` method to query JSON arrays by their length:

```php
$users = DB::table('users')
Expand Down