Skip to content

Kirby 5.0.0

$fieldsets->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.

$fieldsets->group(string|Closure $field, bool $caseInsensitive = true): Kirby\Cms\Fieldsets

Parameters

Name Type Default Description
$fieldrequired stringorClosure no default value
$caseInsensitive bool true Ignore upper/lowercase for group names

Return type

Kirby\Cms\Fieldsets

This method does not modify the existing $fieldsets object but returns a new object with the changes applied. Learn more →

Exceptions

Type Description
Kirby\Exception\Exception

Parent class

Kirby\Cms\Fieldsets inherited from Kirby\Cms\Collection

Examples

<!-- group the collection by the field `category` -->
<?php foreach ($collection->group('category') as $category => $group): ?>
  <h2><?= $category ?></h2>
  <ul>
    <?php foreach ($group as $item): ?>
    <li><?= $item->title() ?></li>
    <?php endforeach ?>
  </ul>
<?php endforeach ?>