2
[
  {"id":4,"sum":19},
  {"id":3,"sum":9},
  {"id":6,"sum":32}, 
  {"id":2,"sum":32}
]

I'm trying this, but it finds only by 1 value, how to find by sum of "sum" values?

 $query->whereJsonContains('programs', [['sum' => intval($search)]]);
2
  • your query has a where clause, so it will find the row where it has the integer you defined, and only that value. Commented Jan 22, 2020 at 15:40
  • can you explain more about what you're trying to achieve? Commented Jan 22, 2020 at 15:42

2 Answers 2

3

This is now supported on eloquent. This worked for me on Laravel 8 using MySQL 5.7:

Entity::sum('json_column->field')
Sign up to request clarification or add additional context in comments.

Comments

0

You may use

//...
->whereRaw(
'JSON_CONTAINS(programs->"$[*].sum", "' . intval($search) . '")')->get();

See related

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.