# $cart->group()

> Groups the elements by a given field or callback function. Returns a new collection with an element for each group and a subcollection in each group

- Source: `src/Toolkit/Collection.php#L543` <https://github.com/getkirby/kirby/blob/6.0.0-beta.1/src/Toolkit/Collection.php#L543>
- Inherited from Kirby, not part of the Merx API. See <https://getkirby.com/docs/reference>.

```php
$cart->group(string|Closure $field, bool $caseInsensitive = true): Kirby\Toolkit\Collection
```

## Parameters

| Name | Type | Default |
| --- | --- | --- |
| `$field` | `string\|Closure` | `–` |
| `$caseInsensitive` | `bool` | `true` |

## Return type

`Kirby\Toolkit\Collection`

## Exceptions

| Type | Description |
| --- | --- |
| `Exception` | if $field is not a string nor a callback function |

## Parent class

`Wagnerwagner\Merx\Cart` inherited from `Kirby\Toolkit\Collection`
