0

Inside users table I have a json column named "agencies" that stores data as a simple array like this:

[
"0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12",
"f7c748d4-8718-441e-aa69-91b890ead5ed"
],

the above is valid json. When I try to select all users that contain 0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12 I get null

Is my query correct?

$users = User::whereRaw('JSON_CONTAINS(agencies->"$[*]", "0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12")')->get();

is the below correct way to do write JSON select query conbsidering how I store uuids as an array inside agencies column which is defined as json?

'JSON_CONTAINS(agencies->"$[*]", "0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12"

I got the idea for above select from reading: Making a Laravel 5.4 query on a JSON field containing a JSON array

but the solution in that post is different then what I am trying to do and my modification to it does not give me back any users, but instead I allways get null back.

2 Answers 2

0

I believe that's what you want:

$users = User::whereRaw('JSON_CONTAINS(agencies, ?)', ['0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12'])->get();

It will also soon be supported with the query builder. See PR

Sign up to request clarification or add additional context in comments.

1 Comment

that looked like it would be the right syntax, but I still got null back. Wonder why it fails.
0

MySQL expects a JSON string:

$users = User::whereRaw('JSON_CONTAINS(agencies, ?)',
    ['"0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12"'])->get();
$users = User::whereRaw('JSON_CONTAINS(agencies, ?)',
    [json_encode('0eb2edf0-50cb-44ff-a0a6-b2a104a9dc12')])->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.