# $pages-\>groupBy($field) syntax

**URL:** <https://forum.getkirby.com/t/pages-groupby-field-syntax/896>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 24, 2015, 4:19pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896 "2015-04-24T16:19:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![FabianSperrle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabiansperrle/32/5426_2.png) [@FabianSperrle](https://forum.getkirby.com/u/FabianSperrle)\
**Post date:** [April 24, 2015, 4:19pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/1 "2015-04-24T16:19:30Z")

</div>

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:

```auto
$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](https://github.com/getkirby/kirby/issues/217 "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?

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [April 29, 2015, 12:29pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/2 "2015-04-29T12:29:43Z")

</div>

I think it is still a bug (or simply missing functionality), which still has to be implemented in a future version of Kirby.

---

<div class="post-metadata">

**Author:** ![ik\_arno](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@ik\_arno](https://forum.getkirby.com/u/ik_arno)\
**Post date:** [April 29, 2015, 12:35pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/3 "2015-04-29T12:35:59Z")

</div>

Would pluck do the trick? [http://getkirby.com/docs/cheatsheet/files/pluck](http://getkirby.com/docs/cheatsheet/files/pluck)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 29, 2015, 12:40pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/4 "2015-04-29T12:40:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [April 29, 2015, 12:40pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/5 "2015-04-29T12:40:19Z")

</div>

That could work. Something like (untested):

```php
$collection = $page->children()->visible();
$years = $collection->pluck('year', null, true);
foreach ($years as $year):
    echo '<h2>'.$year.'</h2>';
    $reports = $collection->filterBy('year', $year);
    foreach ($reports as $report):
        // display some stuff
    endforeach;
endforeach;

```

---

<div class="post-metadata">

**Author:** ![FabianSperrle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabiansperrle/32/5426_2.png) [@FabianSperrle](https://forum.getkirby.com/u/FabianSperrle)\
**Post date:** [May 1, 2015, 2:17pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/6 "2015-05-01T14:17:54Z")

</div>

Thanks for all your hints and tips, I’ll give that it a try.

> [@distantnative](#):
>
> or simply missing functionality

If this is actually not implemented, then it might be a good idea to remove it from the documentation: [http://getkirby.com/docs/cheatsheet/pages/groupBy](http://getkirby.com/docs/cheatsheet/pages/groupBy) @bastianallgeier

---

<div class="post-metadata">

**Author:** ![Stolzenhain](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/stolzenhain/32/4773_2.png) [@Stolzenhain](https://forum.getkirby.com/u/Stolzenhain)\
**Post date:** [July 16, 2015, 9:51am UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/7 "2015-07-16T09:51:11Z")

</div>

> [@FabianSperrle](#):
>
> If this is actually not implemented, then it might be a good idea to remove it from the documentation: [http://getkirby.com/docs/cheatsheet/pages/groupBy](http://getkirby.com/docs/cheatsheet/pages/groupBy) @bastianallgeier

Here’s the corresponding issue, I agree that it’s a bit confusing when offered in the docs:

> <https://github.com/getkirby/kirby/issues/217>
>
> https://github.com/filp/whoops

---

<div class="post-metadata">

**Author:** ![roo](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roo/32/1445_2.png) [@roo](https://forum.getkirby.com/u/roo)\
**Post date:** [February 12, 2016, 3:23am UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/8 "2016-02-12T03:23:07Z")

</div>

Bump.

Seems that groupBy is still not working?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 12, 2016, 7:59am UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/9 "2016-02-12T07:59:03Z")

</div>

It should work, have you seen[the docs for the correct syntax](https://getkirby.com/docs/cheatsheet/pages/group-by)?

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [June 30, 2016, 8:45am UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/10 "2016-06-30T08:45:13Z")

</div>

Seems to work, but I get this:

> Invalid grouping value for key: some/page

So it’s required to have a value for every item in the groupBy collection. Maybe it would be better to just skip it if it does not exist?

Is it possible to skip items that does not have values in a kind of one-liner?

**Current state**

```php
$categories = page('somepage')->children()->groupBy('category');

```

Not all pages have the value `category`.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 30, 2016, 8:46am UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/11 "2016-06-30T08:46:36Z")

</div>

You could filter before you group.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [June 30, 2016, 8:52am UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/12 "2016-06-30T08:52:14Z")

</div>

Yes! This seems to work:

```php
$filtered = page('somepage')->children()->filterBy('category', '!=', '');
$categories = $filtered->groupBy('category');

```

Thanks! 😃

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:57pm UTC](https://forum.getkirby.com/t/pages-groupby-field-syntax/896/13 "2025-02-06T16:57:38Z")

</div>


