Skip to content

Conversation

staudenmeir
Copy link
Contributor

Laravel uses the -> operator to query JSON columns on MySQL:

select * from `users` where `options`->'$."language"' = ?

This operator is specific to MySQL and not available on MariaDB. We can support both databases by using the JSON_EXTRACT() function instead (MySQL, MariaDB):

select * from `users` where json_extract(`options`, '$."language"') = ?

From the MySQL documentation:

The -> operator serves as an alias for the JSON_EXTRACT() function [...]

The ->> operator can be used to unquote the JSON result (User::select('options->>language')). In this case, we have to wrap JSON_EXTRACT() in JSON_UNQUOTE() (MySQL, MariaDB).

For whereJsonContains() we have to split the JSON selector into the field and the path and pass the optional path as the third parameter (MySQL, MariaDB).

whereJsonLength() already supports MariaDB.

This is a breaking change for people who select JSON values without a column alias:

$user = User::select('options->language')->first();
dd($user->{'`options`->\'$."language"\''});

This PR also simplifies SQLiteGrammar and SqlServerGrammar by using wrapJsonFieldAndPath().

@X-Coder264
Copy link
Contributor

@staudenmeir Could you please document this breaking change in the upgrade docs for 5.8?

@wimil
Copy link

wimil commented Oct 30, 2019

support laravel 6 ?

@richbowen
Copy link

Is this available in laravel 7 and 8?

@staudenmeir
Copy link
Contributor Author

@rgb-one Yes.

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 participants