mysql - 将 MySQL (JSON) 查询转换为 Laravel Eloquent
问题描述
我正在尝试将 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"),
"Removed")')->
get();
推荐阅读
- android - 点击通知未启动应用程序
- wordpress - 如何在 WPML 输出之前添加指向菜单的链接?
- javascript - 将 React JSX 与使用 Jest/Enzyme 生成的 HTML 进行比较
- tensorflow - 训练没有完成它在 Tensorflow 2.0 中的第一个 epoch
- vue.js - Vue 应用程序中的 d3 - 如何在地图上应用缩放
- html - 将一个 div 元素均匀地覆盖在其他两个元素之上
- laravel - 将vue js文件导入laravel
- java - JPA EclipseLink 有时会重复实体
- python - 主进程退出后如何保持子进程运行?
- jax-rs - 关闭 Apache Aries 的默认网页