Reference

Guide

$pages->group()

Groups the items by a given field or callback. Returns a collection with an item for each group and a collection for each group.

$pages->group(string|\Closure $field, bool $i = true): Kirby\Cms\Collection

Parameters

Name Type Default Description
$field string|Closure
$i bool true

Ignore upper/lowercase for group names

Return type

Kirby\Cms\Collection

Exceptions

Type Description
Kirby\Exception\Exception

Example

// return collection of pages grouped by the first character of their title field
$groups = $page->children()->sortBy('title', 'asc')->group(function($item) {
    return Str::substr($item->title()->value(), 0, 1);
});

More information

Grouping collections

Inherited from

Kirby\Cms\Collection

Source code

kirby/src/Cms/Collection.php#L131