Skip to content
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

[9.x] Add query builder method whereJsonContainsKey() #41802

Merged

Conversation

derekmd
Copy link
Contributor

@derekmd derekmd commented Apr 3, 2022

Resubmit of draft #41756 for new method whereJsonContainsKey(). This PR also allows checking for array integer keys, supports SQLite, and runs integration tests on each database driver.

DB::table('users')
    ->whereJsonContainsKey('options->languages')
    ->get();

DB::table('users')
    ->whereJsonDoesntContainKey('options->language->primary')
    ->get();

DB::table('users')
    ->whereJsonContainsKey('options->2fa[0]')
    ->get();

DB::table('users')
    ->whereJsonDoesntContainKey('options->2fa[0][1]')
    ->get();

@derekmd derekmd force-pushed the query-where-json-contains-key-array-support branch from f14d666 to 3c0398f Compare April 3, 2022 03:23
Allow filtering by JSONB documents that contain a given object string
key or an array integer key.
@derekmd derekmd force-pushed the query-where-json-contains-key-array-support branch from 3c0398f to 99bb6ca Compare April 3, 2022 21:47
@taylorotwell taylorotwell merged commit 756097c into laravel:9.x Apr 4, 2022
@taylorotwell
Copy link
Member

Thanks!

@derekmd derekmd deleted the query-where-json-contains-key-array-support branch April 4, 2022 15:46
@tpetry
Copy link
Contributor

tpetry commented Apr 6, 2022

@derekmd Is there a specific reason for the complex PostgreSQL implementation instead of just using jsonb_path_exists like for MySQL?

@derekmd
Copy link
Contributor Author

derekmd commented Apr 6, 2022

@tpetry

https://laravel.com/docs/9.x/database#introduction

Laravel 9.x officially supports PostgreSQL 10.0+ but jsonb_path_exists() wasn't added until 12:

Whereas Laravel supports MySQL 5.7+ and 5.7 has json_contains_path(): https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-contains-path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants