【发布时间】:2020-09-19 11:57:12
【问题描述】:
我正在尝试将 MySQL 查询(其中包含 mysql-json 函数)转换为 Laravel Eloquent 查询。下面是我的 Mysql 查询。我必须在 laravel 5.3 版中构建这个雄辩的查询。尝试并坚持使用 json 函数
MYSQL 查询:
SELECT id FROM `house_json` WHERE JSON_CONTAINS( JSON_EXTRACT(construction_json, "$.house.room[*].window"), '"Removed"' ) AND form_id=5 AND action_date BETWEEN '2020-05-25' AND '2020-05-27'
雄辩的查询:
$data_query = HouseData::select('house_json.id as ID')
->where('house_json.form_id',5)
->whereBetween('house_json.action_date', ['2020-05-25', '2020-05-27']) ->whereRaw('json_contains("json_extract("house_json.construction_json", "$.house.room[*].window")", '"Removed"')')
->get();
示例 JSON(存储在名为 - construction_json 的表字段中):
{
"visit_date": "2020-05-25",
"operative_name": "Peter",
"tenant_name": "Denny",
"id": "433",
"house": {
"room": [
{
"appliance_id": "72329",
"landlord": "Yes",
"location_id": 4,
"location_name": "Back Hall",
"status": "Removed",
"comment": "",
"reason": "Landlord denied"
},
{
"appliance_id": "72330",
"landlord": "Yes",
"location_id": 4,
"location_name": "Kitchen",
"status": "Completed",
"comment": "",
"reason": ""
}
],
"other_detail": {
"pipework_done": "Yes",
"paperwork_done": "No",
"general_comments": ""
}
}
}
【问题讨论】:
-
到目前为止你有什么,你在哪里卡住了?
-
我已将查询转换为: $data_query = HouseData::select('house_json.id as ID') ->where('house_json.form_id',5) ->whereBetween('house_json. action_date', ['2020-05-25', '2020-05-27']) ->whereRaw('json_contains("json_extract("house_json.construction_json", "$.house.room[*].window") ", '"删除"')') ->get();我被困在 json_contains 和 json_extract 部分,我必须在 laravel 5.3 版中构建这个查询
-
不错。你能用那个代码编辑你的问题吗?以正确的格式阅读它要容易得多。此外,一些示例 JSON 数据也可能有助于理解您的问题。
-
@JorisJ1 我已经更新了更多细节的问题。示例 json 是我存储到表中的内容