Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 19, 2024
1 parent 39713c9 commit 9ceeb68
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ You may format the relatable models. By default the ID is displayed, but you can
$field->display('name');
```

Alternatively, you may pass a `Closure` to have an apply formatting logic:
Alternatively, you may pass a `Closure`:

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

#### Aggregates

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.

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.

```php
$field->aggregate('count', 'id');

// or

$field->aggregate('sum', 'tax');
```

> The available aggregate functions: `count`, `min`, `max`, `sum`, `avg`.
#### Grouping

You may also group options for a better UI. This feature wraps the `<option>` elements into an `<optgroup>`.

```php
$field->groupOptionsBy('category_id');
```

Alternatively, you may pass a `Closure`:

```php
$field->groupOptionsBy(static function (Model $model): string {
return $model->category->name;
});
```

#### Subresources

### BelongsTo
Expand Down

0 comments on commit 9ceeb68

Please sign in to comment.