Laravel sql 搜索 json 类型列
Laravel sql search json type column
mysql有table'subscribe'table如下:
column type
id int
condition json
type_id id
示例如下:
"id": "1",
"condition": "{\"id\":\"2\",\"class\":\"master\",\"zone\":\"west\",\"price\":\"511\"}",
"type_id": "1"
我想 select 在列条件中匹配的列,如 "zone"="west"
laravel 5.2 已支持订单
$subscribe = DB::table('subscribe')->where('condition->class', 'master')->get();
错误
Column not found: 1054 Unknown column 'condition->class' in 'where clause'(
SQL: select * from `subscribe` where `condition->class` = master)
我需要的是获得匹配条件的术语 where conditon->class = master 。我需要 select 匹配模型中需要的数据。
我不知道出了什么问题。任何帮助将不胜感激。
问题是查询中的 where 子句被视为条件->class 中的列名,而不是 laravel 代码。你想从 json 中提取值。但查询不理解。
您需要做的是以 json 格式将整个 table 传递给视图,然后在视图中提取。
我建议这样做:
在这里,$subscribe 在 json 中包含整个 table。您可以通过以下方式访问它:
$subscribe = DB::table('subscribe')->all();
return response()->json(array('subscribe' => $subscribe));
然后在视图中执行:
@if($subscribe->condition->class == 'master')
id : {{ $subscribe->id }}
type id : {{ $subscribe->type_id }}
@endif
更新代码
//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
}
像这样就可以了
我认为正确的语法是:
$subscribe = DB::table('subscribe')->where('condition->"$.class"', 'master')->get();
参见本段下方的示例,在 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). You can use such expressions in place of column identifiers wherever they occur in SQL statements.
mysql有table'subscribe'table如下:
column type
id int
condition json
type_id id
示例如下:
"id": "1",
"condition": "{\"id\":\"2\",\"class\":\"master\",\"zone\":\"west\",\"price\":\"511\"}",
"type_id": "1"
我想 select 在列条件中匹配的列,如 "zone"="west" laravel 5.2 已支持订单
$subscribe = DB::table('subscribe')->where('condition->class', 'master')->get();
错误
Column not found: 1054 Unknown column 'condition->class' in 'where clause'(
SQL: select * from `subscribe` where `condition->class` = master)
我需要的是获得匹配条件的术语 where conditon->class = master 。我需要 select 匹配模型中需要的数据。 我不知道出了什么问题。任何帮助将不胜感激。
问题是查询中的 where 子句被视为条件->class 中的列名,而不是 laravel 代码。你想从 json 中提取值。但查询不理解。
您需要做的是以 json 格式将整个 table 传递给视图,然后在视图中提取。
我建议这样做: 在这里,$subscribe 在 json 中包含整个 table。您可以通过以下方式访问它:
$subscribe = DB::table('subscribe')->all();
return response()->json(array('subscribe' => $subscribe));
然后在视图中执行:
@if($subscribe->condition->class == 'master')
id : {{ $subscribe->id }}
type id : {{ $subscribe->type_id }}
@endif
更新代码
//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
}
像这样就可以了
我认为正确的语法是:
$subscribe = DB::table('subscribe')->where('condition->"$.class"', 'master')->get();
参见本段下方的示例,在 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). You can use such expressions in place of column identifiers wherever they occur in SQL statements.