0

Sample Data

[{
    "name": "aml_kyc",
    "order": 4,
    "online": true,
    "enabled": true,
    "completed": false
}, {
    "name": "payment_video",
    "order": 6,
    "online": false,
    "enabled": true,
    "completed": false
}]

I tried $project->where('project_progress->name', 'aml_kyc')->first() but I get null

Laravel@latest [email protected]

11
  • can u post your table structure. Commented Feb 1, 2020 at 11:28
  • How will that help? Commented Feb 1, 2020 at 11:28
  • are u putting this data in a json field? Commented Feb 1, 2020 at 11:29
  • Of course I am. Commented Feb 1, 2020 at 11:29
  • Does this answer your question? Laravel Eloquent - Working with an array of JSON objects Commented Feb 1, 2020 at 16:12

2 Answers 2

0

Because your condition query is wrong, and your json is nested by array.

Try to use JSON_CONTAINS

 $project->whereRaw('json_contains(project_progress, ?)', ['{"name":"aml_kyc"}'])->first()
Sign up to request clarification or add additional context in comments.

9 Comments

Still returning null.
The column name is project_progress. It is a JSON column.
Still the same. null
Now it returns the whole data, as in, the entire row in the database.
@AliGajani json_contains only return the row that project_progress contains {"name":"aml_kyc"}
|
-1
protected $casts = [
    'project_progress' => 'array',
];

3 Comments

I want to be able to query and update some values though. PS: I am already doing casting.
can you try with whereJsonContains instead of where? what is your mySQL version?
My MySQL version is 5.7.27

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.