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.7] Add Builder::whereJsonLength() #25047

Merged
merged 3 commits into from
Sep 6, 2018
Merged

[5.7] Add Builder::whereJsonLength() #25047

merged 3 commits into from
Sep 6, 2018

Conversation

staudenmeir
Copy link
Contributor

@staudenmeir staudenmeir commented Aug 1, 2018

I've seen multiple people asking about queries on JSON columns that find (non-)empty arrays.
This PR brings whereJsonLength() to MySQL 5.7, PostgreSQL 9.3, SQLite 3.9.0 and SQL Server 2016:

DB::table('users')->whereJsonLength('options->languages', 0)->get();
DB::table('users')->whereJsonLength('options->languages', '>', 0)->get();

On MySQL and SQL Server, this can also be used to count the keys in JSON objects.

The MySQL implementation is already designed to work on MariaDB.
wrapJsonFieldAndPath() returning an array instead of a string is also a preparation for bringing JSON support to MariaDB (commit for 5.7).

As with the other JSON query features, there are no integration tests (#24330, #24672, #25328).

@GrahamCampbell GrahamCampbell changed the title [5.6] Add Builder::whereJsonLength() [5.7] Add Builder::whereJsonLength() Sep 3, 2018
@GrahamCampbell GrahamCampbell changed the base branch from 5.6 to 5.7 September 3, 2018 09:39
*/
protected function compileJsonLength($column, $operator, $value)
{
list($field, $path) = $this->wrapJsonFieldAndPath($column);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe will be better to use [$field, $path] instead list($field, $path) ?

@taylorotwell taylorotwell merged commit f149fbd into laravel:5.7 Sep 6, 2018
@taylorotwell
Copy link
Member

It would be nice to have a section in the documentation that demonstrate all of these JSON methods. @staudenmeir do you think you could start a PR for that?

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