How to transform a model using php (laravel) fractal to get data grouped by a condition?

304 Views Asked by At

I have a table as follows :-

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

I'm using fractals in a laravel application that I'm developing. How do I group data to get following response either as json or array?

{
    "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

There are 0 best solutions below