具有复杂json的Laravel Datatable

时间:2019-12-08 11:56:05

标签: datatables laravel-6 laravel-datatables

我有一个名为“项目”的表,该表与laravel模型中由

定义的名为“成员”的表具有一对多的关系。
public function members(){
    return $this->hasMany(Members::class);
}

成员表架构为

id  |  project_id | name  | email  | type_id

type_id是一个枚举,用于定义成员是经理还是普通用户,分别由“ M”和“ G”定义。

现在,当我雄辩地查询表时,它会产生如下结果:

[
    {
        "project_id": "c79bbde-aecd-11e9-81c6-0a0027000009",
        "project_name": "rerum",
        "project_description": "Ut hic culpa dolorem qui velit.",
        "members": [
            {
                "project_id": 1,
                "name": "John Doe",
                "email": "john@example.com",
                "field_type": "M"
            },
            {
                "project_id": 1,
                "name": "Larry K",
                "email": "larry@example.com",
                "field_type": "G"
            },
            {
                "project_id": 1,
                "name": "Peter C",
                "email": "peter@example.com",
                "field_type": "G"
            }
        ]
    },
]

问题是,我想在搜索结果中在数据表中显示上述结果,如下所示:

Project Name | Project Description | Managers | General Users
Rerum        | Ut hic culpa....    | John Doe | Larry K, Peter C

我尝试修改从控制器返回的json并隐藏多余的列,但是数据表在搜索过程中失败。有什么办法可以在不更改查询的情况下实现上述结果?或者无论如何,我可以重写雄辩的结果,如下所示:

[
    {
        "project_id": "c79bbde-aecd-11e9-81c6-0a0027000009",
        "project_name": "rerum",
        "project_description": "Ut hic culpa dolorem qui velit.",
        "Managers": [
            {
                "project_id": 1,
                "name": "John Doe",
                "email": "john@example.com",
                "field_type": "M"
            },
        ],
        "General": [
            {
                "project_id": 1,
                "name": "Larry K",
                "email": "larry@example.com",
                "field_type": "G"
            },
            {
                "project_id": 1,
                "name": "Peter C",
                "email": "peter@example.com",
                "field_type": "G"
            }
        ],
    },
]

所以我可以直接在数据表中解析它们?

P.S:如果它可以与搜索一起使用,我也可以接受原始查询。

0 个答案:

没有答案