Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pooot's avatar

Grouping data with Fractal

Hi!

I'm having some trouble with Fractal and how to group items by date.

I'm using this code:

$items = Schedule::with( 'activity' )
                 ->orderBy( 'start' )
                 ->get();

$days = $items->groupBy( function ( $date ) {
    return $date->start->format( 'Y-m-d' );
} );

Which gives me this result:

Collection {#260 ▼
  #items: array:3 [▼
    "2015-05-14" => array:7 [▼
      0 => Schedule {#264 ▶}
      1 => Schedule {#265 ▶}
      2 => Schedule {#266 ▶}
      3 => Schedule {#267 ▶}
      4 => Schedule {#268 ▶}
      5 => Schedule {#269 ▶}
      6 => Schedule {#270 ▶}
    ]
    "2015-05-15" => array:7 [▶]
    "2015-05-16" => array:10 [▶]
  ]
}

So far it's all good, now i want to pass this to Fractal to clean up the output and remove unnecessary stuff.

Example code, shortened to illustrate:

$resource = new Collection( $days, function (  ) {
            return [
                'date'   => $day,
                'events' => new Collection( $day, function ( Schedule $schedule ) {
                    return [
                        'id'       => (int) $schedule->id,
                        --- snip ---
                        'activity' => [
                            'category'         => $schedule->activity->category,
                            --- snip ---
                        ],
                    ];
                } )
            ];
        } );

This returns

{"data":[{"date":"","events":{}},{"date":"","events":{}},{"date":"","events":{}}]}

Any pointers in the right direction is appreciated, this seems like such an easy thing to do but I'm just not seeing the solution at the moment.

0 likes
3 replies
pooot's avatar

Something like this, pieced together from other responses from Fractal so there might be some errors.

"data": [
    {
        "date": "YYYY-MM-DD",
        "events": [
            {
                "id": 0,
                "start": "YYYY-MM-DD H:i",
                "end": "YYYY-MM-DD H:i",
                "comment": "",
                "data": [
                    {
                        "activities": {
                            "data": [
                                {
                                    "id": 0,
                                    "category": "",
                                    "name": "",
                                    "slug": "",
                                    "excerpt": "",
                                    "description": "",
                                    "rules": "",
                                    "min_participants": 0,
                                    "max_participants": 2,
                                    "links": [
                                        {
                                            "rel": "",
                                            "uri": ""
                                        }
                                    ],
                                    "header": {
                                        "data": {
                                            "id": ,
                                            "name": "",
                                            "description": "",
                                            "url": "",
                                            "thumb": "",
                                            "links": [
                                                {
                                                    "rel": "",
                                                    "uri": ""
                                                }
                                            ]
                                        }
                                    },
                                    "tags": {
                                        "data": [
                                            {
                                                "name": "",
                                                "slug": ""
                                            },
                                            {
                                                "name": "",
                                                "slug": ""
                                            }
                                        ]
                                    }
                                },
                            ]
                        }
                    }
                ]
            } 
        ]
    }
]
davorminchorov's avatar

Just a quick example, don't forget to cast the data to its proper data type. $this->events->field_name is just an example but I am guessing you are getting that data differently.

return [
    // inside data = []; in the response::json() array

    'date' => $date,
    'events' => [
        'id' => (int) $this->events->id,
        'start' => $this->events->start_date,
        'end' => $this->events->end_date,
        'comment' => $this->events->comment,
        data => [
            'activities' => [
                'id' => (int) $this->events->activities()->id,
                'category' => $this->events->activities()->category,
                            'name' => $this->events->activities()->name,
                            'slug' => $this->events->activities()->slug,
                            'excerpt' => $this->events->activities()->excerpt,
                            'description' => $this->events->activities()->description,
                            'rules' => $this->events->activities()->rules,
                            'minimum_participants' => (int) $this->events->activities()->min_participants,
                            'maximum_participants' => (int) $this->events->activities()->max_participants,
              },
            
        ],
        // if these are specific to the activities of the event 
        'links' => [..],
        'header' => [],
        'tags' => [] 

    ],
    /*  After data = [] in the response::json array
        'meta' => [
            // header = [...],
            // tags = [....],
            // links = [....],
            // pagination = [....]      
        ]
    */
];

Check out the Incremental APIs series here on Laracasts for some extra info.

Please or to participate in this conversation.