Computed#
Computed shows a value your resource calculates, such as a line total
or a full name, on the list and the record page, computed in the
descriptor. It is a read-only value a method of the resource computes
from the record:
Computed::make('lineTotal', 'Total')->as('money')->currencyField('order.currency')
->from(['quantity', 'unitPriceCents'])->method('lineTotal'),
/** @param array<string, mixed> $line the serialized record */
public function lineTotal(array $line): ?int
{
return $line['quantity'] * $line['unitPriceCents'];
}
as(): how it is shown:text,number(decimals()),money(currency()orcurrencyField()),boolean,date,datetime, orselectwithoptions(). The value is typed by it and exported like any field.from(): the fields the method reads: every list and record that shows the computed value carries them too, whatever thecolumnsasked for.method(): a public instance method of the resource, built by the request container and called once per record with the record already serialized. It runs no query of its own, so a list’s statement count does not change.
A computed field is read-only, and never sorted, filtered, searched,
summarized, written or imported. The build refuses one that asks for
any of those, one without a method, one whose method is not a public
instance method, a select without options, and a source that is not a
field holding a value. If a user may not see a field it reads
(viewField()), they do not see the computed field either, so a
derived value never shows what the user may not see. The descriptor
publishes computed: {as, from}; the method’s name stays on the server.