Skip to content

Columns

Columns describe what a list shows per record – in the table, in the cards and in exports. All columns are in Anvil\Collection\Columns and are created with ::make('name'):

TextColumn::make('number')->sortable()->searchable(),
TextColumn::make('amount')->money('EUR')->sortable(),
TextColumn::make('status')->labels(self::STATUSES)->badge(['paid' => 'success']),
CountColumn::make('items')->relation(InvoiceItemModel::class, 'invoice_id'),
Column Use it for
TextColumn text, labels, badges, yes/no, dates, numbers, amounts, referenced records, users
CountColumn the number of related records
SelectColumn changing a value in the table with a selection
ToggleColumn switching a yes/no value in the table
CheckboxColumn ticking a yes/no value in the table
TextInputColumn changing text, a number or a date in the table
Own columns anything else, e.g. a progress bar

The name must be a plain name ([a-z_][a-z0-9_]*, at most 64 characters, otherwise an InvalidArgumentException); the label defaults to the humanized name. The value of a column is:

  1. the result of state(fn ($record) => …) if set (a computed value),
  2. otherwise the attribute of the record with the column name – a column of the model or of a join in scopeQuery().
Option Effect
label('…') Header, card label and export header
sortable() Sortable by its header
searchable() Included in the search
alignEnd() Right aligned (numbers and counts are by default)
toggleable(), toggleable(hidden: true) The user can hide the column; with hidden it starts hidden
queryColumn('t.col', …) Sort and search by other columns, e.g. of a join, see Computed values
state(fn ($record) => …) A computed value instead of a column of the record
color(…) Colour of the value, see Colours and icons
icon('calendar-event') Bootstrap icon before the value
description('email') A second, smaller line under the value (table only)
link('hr', 'person/{value}') Link to an external system, see Links

Every column produces its value as HTML (html(), escaped), and render() puts the link, the icon and the colour around it – the same in table rows, cards and own card views. Empty values show nothing. All values are escaped; a column cannot output HTML from the database.

A sortable column sorts by its database column, or by its queryColumn()s in order. Computed columns are only sortable with queryColumn(); sortable() alone has no effect for them. The default order is defaultSort() of the collection, otherwise newest first. Only declared, sortable columns can be requested; other values are rejected (422).

toggleable() columns appear in the column menu of the toolbar. What a user hides is remembered per user and list, see Saved views. Hidden columns are left out of the cards as well. Columns that are not toggleable are always shown.

Exports use the same columns: the header is the label, the value is exportValue() – numbers for numeric(), money() and CountColumn, the shown text otherwise.