Skip to content

Commit 9ceeb68

Browse files
committed
wip
1 parent 39713c9 commit 9ceeb68

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

fields.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ You may format the relatable models. By default the ID is displayed, but you can
560560
$field->display('name');
561561
```
562562

563-
Alternatively, you may pass a `Closure` to have an apply formatting logic:
563+
Alternatively, you may pass a `Closure`:
564564

565565
```php
566566
$field->display(static function (Model $model): string {
@@ -570,8 +570,36 @@ $field->display(static function (Model $model): string {
570570

571571
#### Aggregates
572572

573+
On `index` views, often it makes more sense to display an aggregated value of the relation. For example, when a model has a `BelongsToMany` relation with a bunch of attached record, it's not really optimal to show all the related model's formatted value in the table row, but the number of the attached values.
574+
575+
You may customize this aggregation logic using the `aggregate` function. The first parameter is the aggregate function, while the second one is the aggregated column.
576+
577+
```php
578+
$field->aggregate('count', 'id');
579+
580+
// or
581+
582+
$field->aggregate('sum', 'tax');
583+
```
584+
585+
> The available aggregate functions: `count`, `min`, `max`, `sum`, `avg`.
586+
573587
#### Grouping
574588

589+
You may also group options for a better UI. This feature wraps the `<option>` elements into an `<optgroup>`.
590+
591+
```php
592+
$field->groupOptionsBy('category_id');
593+
```
594+
595+
Alternatively, you may pass a `Closure`:
596+
597+
```php
598+
$field->groupOptionsBy(static function (Model $model): string {
599+
return $model->category->name;
600+
});
601+
```
602+
575603
#### Subresources
576604

577605
### BelongsTo

0 commit comments

Comments
 (0)