I have a page in which I collect reports as sub-pages. Amongst other fields, those reports have a field called ‘year’. As I want to display some information in per-year blocks, I wanted to use the groupBy method like this:
$reports = $page->children()->visible()->groupBy('year');
foreach ($reports as $group):
foreach ($group as $report):
// display some stuff
endforeach;
endforeach;
However that. does not work. Instead I get an exception with the message “You cannot group by arrays or objects”. I found an issue on GitHub stating “$pages->groupBy($field) doesn’t work on pages?”.
So, what’s the matter with this? Is my code wrong or is there actually a bug in the toolkit?
You could use pluck()to get an array of unique years and then iterate through this array and check for each page if the condition year = x is true. That should work.