Search code examples
laraveleloquenttransformer-model

Grouping Object Array


Hello I have the following JSON:

[
  {
    "id": 1,
    "company_id": 2,
    "route_id": 16,
    "class": "Standard",
    "fare": 35,
    "weekday": "monday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 2,
    "company_id": 2,
    "route_id": 16,
    "class": "Standard",
    "fare": 35,
    "weekday": "tuesday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 3,
    "company_id": 2,
    "route_id": 16,
    "class": "Standard",
    "fare": 35,
    "weekday": "wednesday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 4,
    "company_id": 2,
    "route_id": 16,
    "class": "VIP",
    "fare": 35,
    "weekday": "thursday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 5,
    "company_id": 2,
    "route_id": 16,
    "class": "VIP",
    "fare": 35,
    "weekday": "friday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 6,
    "company_id": 2,
    "route_id": 16,
    "class": "Business",
    "fare": 35,
    "weekday": "saturday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  }
]

This is how the data is being returned by my eloquent model, I format the output with transformers to hide certain fields etc.

I would like to group this data by the class property. This is how I want the data to look like:

[
   {
      "class":"Standard",
      "schedules":[
         {
            "id":1,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"monday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         },
         {
            "id":2,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"tuesday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         },
         {
            "id":3,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"wednesday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         }
      ]
   },
   {
      "class":"VIP",
      "schedules":[
         {
            "id":4,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"thursday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         },
         {
            "id":5,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"friday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         }
      ]
   },
   {
      "class":"Business",
      "schedules":[
         {
            "id":6,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"saturday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         }
      ]
   }
]

How do I do this without altering the eloquent query or database structure? Is there a way I can use transformers to achieve this? If not and my only option is to change the eloquent query how would I do that to get the data looking like the way I want it?


Solution

  •     $result=[];
        foreach($youarray as $key => $item)
        {
           $result[ $item['class'] ]['class'] = $item['class'];
           $result[ $item['class'] ]['schedules'] = [$item['id'], $item['company_id'], etc. ];
        } 
        return $result;