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.8] Fix JSON boolean queries #27847

Merged
merged 3 commits into from Mar 11, 2019

Conversation

Projects
None yet
3 participants
@staudenmeir
Copy link
Contributor

staudenmeir commented Mar 10, 2019

Laravel 5.8's unquoting of JSON values on MySQL (#25732) broke boolean comparisons:

DB::table('users')->where('items->available', '=', true)->get();

// expected
select * from `users` where json_extract(`items`, '$."available"') = true

// actual
select * from `users` where json_unquote(json_extract(`items`, '$."available"')) = true

The json_unquote() call is actually only necessary for the SELECT clause, but I don't see a feasible/elegant way to compile different statements for the SELECT and the WHERE clause.

So I suggest we fix this by adding a new JsonBoolean type.
The default implementation works on SQLite 3.23+.

The is_string($column) check is necessary to preserve raw expressions:

->where(DB::raw("items->'$.available'"), '=', true)

I used the opportunity to also add support for PostgreSQL and SQL Server.

Fixes #27696.

@taylorotwell taylorotwell merged commit 9a24ca4 into laravel:5.8 Mar 11, 2019

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@staudenmeir staudenmeir deleted the staudenmeir:json-boolean branch Mar 11, 2019

@aaronhuisinga

This comment has been minimized.

Copy link
Contributor

aaronhuisinga commented Mar 12, 2019

Thanks for the fix, @staudenmeir! Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.