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
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

数据:

[
    {
        "id": 1,
        "extra": {
            "assist": [],
            "manager": {
                "construction": [
                    {
                        "user_id": 10
                    },
                    {
                        "user_id": 20
                    }
                ]
            }
        },
        "created_at": "2019-12-23 14:54:03",
        "updated_at": "2019-12-23 14:54:03"
    },
    {
        "id": 2,
        "extra": {
            "assist": [],
            "manager": {
                "construction": [
                    {
                        "user_id": 10
                    },
                    {
                        "user_id": 30
                    }
                ]
            }
        },
        "created_at": "2019-12-23 14:55:03",
        "updated_at": "2019-12-23 14:55:03"
    },
    {
        "id": 3,
        "extra": {
            "assist": [],
            "manager": {
                "construction": [
                    {
                        "user_id": 20
                    }
                ]
            }
        },
        "created_at": "2019-12-23 14:55:13",
        "updated_at": "2019-12-23 14:55:13"
    }
]

查询:

$projects = \App\Models\Project::query()->whereJsonContains('extra->manager->construction', ['user_id' => 10])->get();

return $projects;
5年前 评论
讨论数量: 2
Epona

postgresql 的话 提供了原生的 json 方法, 你可以找找mysql的话有没有json查找方法,如果有的话,就可以用。whereRaw来进行查找。

5年前 评论

数据:

[
    {
        "id": 1,
        "extra": {
            "assist": [],
            "manager": {
                "construction": [
                    {
                        "user_id": 10
                    },
                    {
                        "user_id": 20
                    }
                ]
            }
        },
        "created_at": "2019-12-23 14:54:03",
        "updated_at": "2019-12-23 14:54:03"
    },
    {
        "id": 2,
        "extra": {
            "assist": [],
            "manager": {
                "construction": [
                    {
                        "user_id": 10
                    },
                    {
                        "user_id": 30
                    }
                ]
            }
        },
        "created_at": "2019-12-23 14:55:03",
        "updated_at": "2019-12-23 14:55:03"
    },
    {
        "id": 3,
        "extra": {
            "assist": [],
            "manager": {
                "construction": [
                    {
                        "user_id": 20
                    }
                ]
            }
        },
        "created_at": "2019-12-23 14:55:13",
        "updated_at": "2019-12-23 14:55:13"
    }
]

查询:

$projects = \App\Models\Project::query()->whereJsonContains('extra->manager->construction', ['user_id' => 10])->get();

return $projects;
5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!