How do you want your json to look like? the var_dump result?
Feb 28, 2015
3
Level 1
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.
Please or to participate in this conversation.