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
Merged

[5.8] Fix JSON boolean queries #27847

merged 3 commits into from
Mar 11, 2019

Conversation

staudenmeir
Copy link
Contributor

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.

@aaronhuisinga
Copy link

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[5.8] Querying on json columns with boolean values broken
3 participants