Laravel 中如何从 JSON 字段中查询数据?
数据库里的extra_attributes字段内容如下。
{
"协助部门人员": {
"设计部负责人": {
"memo": null,
"phone": null,
"user_id": "82"
},
"采购部负责人": {
"memo": null,
"phone": null,
"user_id": "99"
},
"预算部负责人": {
"memo": null,
"phone": null,
"user_id": "91"
}
},
"项目管理人员": {
"劳资员": {
"memo": null,
"phone": null,
"user_id": "27"
},
"施工员": {
"1": {
"memo": null,
"phone": null,
"user_id": "10"
},
"2": {
"memo": null,
"phone": null,
"user_id": "22"
},
"3": {
"memo": null,
"phone": null,
"user_id": "14"
},
"4": {
"memo": null,
"phone": null,
"user_id": "55"
}
},
"材料员": {
"memo": null,
"phone": null,
"user_id": "53"
},
"项目经理": {
"memo": null,
"phone": null,
"user_id": "6"
},
"项目负责人": {
"memo": null,
"phone": null,
"user_id": "7"
},
"安全文明负责人": {
"memo": null,
"phone": null,
"user_id": "31"
},
"技术负责人_投标": {
"memo": null,
"phone": null,
"user_id": "6"
},
"技术负责人_现场": {
"memo": null,
"phone": null,
"user_id": "20"
}
}
}
项目管理人员 下的 施工员 有多个。
我想通过施工员的user_id 来查询记录。
我使用下面的方法,但是查询不出来。应该如何写才是正确的呢?
Project::where('extra_attributes->项目管理人员->施工员->[*]->user_id',22)->get();
上面的方法输出的SQL是
select
*
from
`projects`
where
json_unquote(
json_extract(
`extra_attributes`,
'$."项目管理人员"."施工员"."[*]"."user_id"'
)
) = 22
数据:
查询: