简体   繁体   English

Laravel SQL搜索JSON类型列

[英]Laravel sql search json type column

The mysql has table 'subscribe' table and it's as follows: mysql具有表'subscribe'表,如下所示:

column   type
id        int
condition  json
type_id    id

and the example as follows: 并且示例如下:

"id": "1",
"condition": "{\"id\":\"2\",\"class\":\"master\",\"zone\":\"west\",\"price\":\"511\"}",
"type_id": "1"

and I want to select the column which match in the column condition like "zone"="west" the laravel 5.2 has support the order 并且我想选择在列条件中匹配的列,例如laravel 5.2支持该列的“ zone” =“ west”

 $subscribe = DB::table('subscribe')->where('condition->class', 'master')->get();

Error 错误

 Column not found: 1054 Unknown column 'condition->class' in 'where clause'(
SQL: select * from `subscribe` where `condition->class` = master)

I need is get the term which match the condition where conditon->class = master .I need to select the data which match the need in the model. 我需要的是匹配conditon-> class = master的条件的术语。我需要选择与模型中的需求相匹配的数据。 I don't know what's wrong. 我不知道怎么了 Any help would be much appreciated. 任何帮助将非常感激。

I believe the correct syntax is: 我相信正确的语法是:

 $subscribe = DB::table('subscribe')->where('condition->"$.class"', 'master')->get();

See the example below this paragraph, on https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html 请参见本段下面的示例, 网址为https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

  • column->path 列->路径

In MySQL 5.7.9 and later, the -> operator serves as an alias for the JSON_EXTRACT() function when used with two arguments, a column identifier on the left and a JSON path on the right that is evaluated against the JSON document (the column value). 在MySQL 5.7.9和更高版本中,当与两个参数一起使用时,->运算符用作JSON_EXTRACT()函数的别名,两个参数分别是左侧的列标识符和右侧的JSON路径(针对JSON文档进行评估)(列值)。 You can use such expressions in place of column identifiers wherever they occur in SQL statements. 您可以使用此类表达式代替SQL语句中出现的列标识符。

The thing is the where clause in query is considering as a column name in condition->class and not as a laravel code. 问题是查询中的where子句被视为condition-> class中的列名,而不是laravel代码。 you want to extract the value from json. 您想从json中提取值。 but the query doesn't understand that. 但是查询不明白这一点。

What you need to do is pass entire table as it is in json format to the view and then extract in the view. 您需要做的是将整个表(以json格式)传递给视图,然后将其提取到视图中。

I suggest do something like this : Here, $subscribe has entire table in json . 我建议做这样的事情:在这里,$ subscribe在json中有整个表。 which you can access it by : 您可以通过以下方式访问它:

$subscribe = DB::table('subscribe')->all();

return response()->json(array('subscribe' => $subscribe));

Then in the view do: 然后在视图中执行:

@if($subscribe->condition->class == 'master')

id      : {{ $subscribe->id }}
type id : {{ $subscribe->type_id }}

@endif

Updated Code 更新的代码

//get condition in json
$subscribe = DB::table('subscribe')->select('condition')->get();

then, 

// convert json to array
$subscribe_array = json_decode($subscribe, true);
//  create a new collection instance from the array
$collection_array = collect($subscribe_array);


if($collection_array['class'] == 'master')
{

//do something

}

Something like this do the trick 这样的事情就可以了

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

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