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

[5.6] Add Builder::whereJsonContains() #24258

Closed
wants to merge 2 commits into from
Closed

[5.6] Add Builder::whereJsonContains() #24258

wants to merge 2 commits into from

Conversation

staudenmeir
Copy link
Contributor

At the moment we can query JSON columns for exact matches:

DB::table('users')->where('options->language', 'en')->get();

This PR adds whereJsonContains() with support for MySQL and PostgreSQL:

// Example value in the `options` column: {"languages":["de","en","fr"]}
DB::table('users')->whereJsonContains('options->languages', 'en')->get();
DB::table('users')->whereJsonContains('options->languages', ['en', 'de'])->get();
DB::table('users')->whereJsonContains('options->languages', DB::raw("'\"en\"'"))->get();

The method expects a PHP value and converts it to JSON. Raw expressions are also supported.

The PostgresGrammar replaces the column name's last -> with a ->> . This is required for where(), but has to be undone for whereJsonContains().

Resolves laravel/ideas#296.

@sisve
Copy link
Contributor

sisve commented May 20, 2018

This PR adds whereJsonContains() with support for MySQL and PostgreSQL:

What happens with other database engines? Do we get a NotSupportedException or anything to help the end-user track down why their queries fail? (Or do they work and return something weird?)

@staudenmeir
Copy link
Contributor Author

The queries just fail. I added an exception.

@taylorotwell
Copy link
Member

Asserting the SQL query matches what you expect seems pointless to test. Test that it actually does something useful / correct.

@staudenmeir
Copy link
Contributor Author

@taylorotwell Do you mean integration tests? Do we support these for MySQL/PostgreSQL?

@staudenmeir staudenmeir deleted the where-json-contains branch May 28, 2018 22:39
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.

3 participants