Skip to content

Stat widget

A key figure with icon, e.g. “Open tasks: 12”.

A key figure with icon, e.g. “Open tasks: 12”.

StatWidget::make('Expiring roles')
->value(static fn (): int => (new RoleAssignmentModel())->where('valid_until <=', $in30)->countAllResults())
->description('in the next 30 days')
->icon('hourglass-split')
->color('warning')
->url($this->url('role-assignments'))
->permission('people.view')
->span(3);
Method Default Description
make(string $label) – label above the value
value(Closure $value) – fn (): int|float|string, evaluated when the widget is shown; without a value — is shown
fromList(Closure $value) – fn (Model $records): int|float|string, value from the records of the list, see Above a list
description(string $text) – small line below the value
icon(string $icon) bar-chart Bootstrap Icons name without bi-
color(string $color) primary primary, secondary, success, danger, warning, info; other values throw an InvalidArgumentException
url(string $url) – makes the whole card a link; a path of the application or an http(s) URL, other values (e.g. javascript:) throw
permission(string $permission) – required permission; without one every user of the panel sees it
span(int $span) 3 width on large screens, 2–12
key(string $key) from the label stable key of the layout, see Keys

The value is escaped and shown as text; format numbers in the closure, e.g. with \Anvil\Support\NumberFormat::format($value, 0) for the locale of the user, or use a grouped stats widget, which formats numbers and amounts itself.

// In Panel::widgets(): the resource of the list
$requests = $this->resourceByClass(PurchaseRequestResource::class);
// A count with a link to the list, prefiltered by a preset
StatWidget::make('Overdue requests')->key('overdue-requests')
->value(fn (): int => $requests->query()->where('due_on <', date('Y-m-d'))->where('status', 'open')->countAllResults())
->icon('alarm')->color('danger')
->url($requests->url() . '?preset=overdue')
->permission('purchase-requests.view');
// An amount in the format of the user
StatWidget::make('Volume this month')
->value(static fn (): string => NumberFormat::format((float) (new OrderModel())->selectSum('amount')->where('ordered_on >=', date('Y-m-01'))->first()['amount'], 2, 'EUR'))
->icon('currency-euro');

The fromList() variant takes its value from the records of the current list.