简体   繁体   English

Laravel whereJson包含查询在5.7中不起作用

[英]Laravel whereJsonContains query not working in 5.7

I have been facing a problem in my project. 我的项目一直遇到问题。 I want to search my json data. 我想搜索我的json数据。 But could not achieve to do this. 但是无法做到这一点。 Here is my query 这是我的查询

$variations = User::whereJsonContains('addon->ram', '>', 0)->count();
return $variations;

And My json data is stored in the addon column and the data is 我的json数据存储在addon列中,数据为

{"ram":"87","base-driven":"89"}

In this query I am getting error says 在此查询中我收到错误提示

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误; (SQL: select count(*) as aggregate from users where 0 json_contains( addon ->'$."ram"', ">") (SQL:选择count(*)作为来自users聚合,其中0 json_contains( addon ->'$。“ ram”',“>”)

I google it but could not solve this 我用谷歌搜索但不能解决这个问题

Thanks in advance 提前致谢

It's because of you are providing "87" and "89" as strings. 这是因为您要提供“ 87”和“ 89”作为字符串。 Try to replace it with numbers: 尝试将其替换为数字:

{"ram":87,"base-driven":89}

Just try this 试试吧

$variations = User::where('addon->ram', '>', 0)->count();
return $variations;

and let time know, if you get some error. 如果有错误,请及时告知。

As you are saving it as string, you may need to do something like 将其保存为字符串时,可能需要执行以下操作

WHERE JSON_EXTRACT(addon, "$[ram]") > 0; in Laravel 在Laravel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM