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

[11.x] Add ->whereJsonOverlaps() for mysql #51288

Merged
merged 4 commits into from
May 6, 2024

Conversation

parkourben99
Copy link
Contributor

This feature PR adds json_overlaps for mysql.

I was using ->whereJsonContains() to check if the json field contained a value. This then evolved to check if an array of values overlaps with what is in the database.

json_overlaps

Compares two JSON documents. Returns true (1) if the two document have any key-value pairs or array elements in common. If both arguments are scalars, the function performs a simple equality test. If either argument is NULL, the function returns NULL.

This function serves as counterpart to JSON_CONTAINS(), which requires all elements of the array searched for to be present in the array searched in. Thus, JSON_CONTAINS() performs an AND operation on search keys, while JSON_OVERLAPS() performs an OR operation.

Example usage:

User::whereJsonOverlaps('languages', ['en', 'fr'])->exists();
User::whereJsonDoesntOverlap('languages', ['en', 'fr'])->exists();

@parkourben99 parkourben99 changed the title Add ->whereJsonOverlaps() for mysql [11.x] Add ->whereJsonOverlaps() for mysql May 4, 2024
@staudenmeir
Copy link
Contributor

Did you look into whether and how this is also supported by the other DBMS?

@parkourben99
Copy link
Contributor Author

Did you look into whether and how this is also supported by the other DBMS?

No, only looked at MySql as that is what I needed. Other databases could be supported in another pull request.

@taylorotwell taylorotwell merged commit 2a32afb into laravel:11.x May 6, 2024
28 checks passed
@GonzaloGPF
Copy link

Thanks for this addition!
But I have a question, could you subquery values to search?

I mean, I would like to do following

$q->whereJsonOverlaps('levels', function ($subQuery) use ($user) {
    $subQuery->select('level')
        ->from('user')
        ->where('users.id', $user->id);

The bindings of that sql looks like this:

[
  0 => 1 // the user->id
  1 => "{}" // the subquery
]

So I guess it produces a {} when receiving a clousure or I'm missing something?

Thanks!

@parkourben99
Copy link
Contributor Author

Thanks for this addition! But I have a question, could you subquery values to search?

I mean, I would like to do following

$q->whereJsonOverlaps('levels', function ($subQuery) use ($user) {
    $subQuery->select('level')
        ->from('user')
        ->where('users.id', $user->id);

The bindings of that sql looks like this:

[
  0 => 1 // the user->id
  1 => "{}" // the subquery
]

So I guess it produces a {} when receiving a clousure or I'm missing something?

Thanks!

A callback will not work, as that is not how json_overlaps works

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.

4 participants