如何使用php(laravel)分形转换模型以获取按条件分组的数据?

时间:2018-11-30 10:43:50

标签: php json laravel transformation thephpleague-fractal

我有一张桌子,如下:-

+----+-------------+------------+--------------+
| id | employee_id | contact_no | is_preferred |
+----+-------------+------------+--------------+
|  1 |         001 | 9138975999 |            1 |
|  2 |         001 | 9785626699 |            0 |
|  3 |         002 | 9337875999 |            0 |
+----+-------------+------------+--------------+

我正在开发的laravel应用程序中使用fractals。如何将数据分组以获得json或数组以下响应?

{
    "contacts": {
        "preferred": {
            "0": {
                "id": 1,
                "employee_id": 1,
                "contact_no": 9138975999,
                "is_preferred": 1
            },
            "1": {
                "id": 2,
                "employee_id": 2,
                "contact_no": 9785626699,
                "is_preferred": 1
            }
        },
        "alternate": {
            "0": {
                "id": 1,
                "employee_id": 1,
                "contact_no": 9337875999,
                "is_preferred": 0
            },
            "1": {
                "id": 2,
                "employee_id": 2,
                "contact_no": 9986556699,
                "is_preferred": 0
            }
        }
    }
}

0 个答案:

没有答案