Fields are the core of an admin model. Each field describes a database column, its form input, its validation rules, its relations and how it is shown in the listing table.
The database follows the fields. Run php artisan admin:migrate after every change, see changing fields.

Writing fields

Return the fields from the fields() method of the admin model. Each key is the name of a database column, the value is the field configuration combined with Laravel validation rules. The configuration is written as a string of parameters separated by |, or as an array.

String notation

This notation is recommended for all fields.
app/Models/Product.php
Parsing rules:
  • A parameter without a value is a flag and becomes true, required is required => true.
  • Values are always strings, default:10 becomes '10'.
  • Only the first colon separates the key, so title:Opens at 10:30 or date_format:H:i keep the rest of the value.
  • A repeated parameter becomes an array of values, index:a|index:b is index => ['a', 'b'].
  • Empty parts are ignored (name:Name||required), placeholder: sets an empty string.
  • A value can not contain |. Use the array notation when you need it.
The form generated from a field definition:
Form generated from the field definition

Array notation

Each field can also be an array. Values keep their PHP types, and a value without a key is a flag.
Use real booleans. 'required' => 'true' is a string, it is passed to Laravel validation but the column stays nullable.

Method notation

Validation often differs between creating a record and editing it. The fields() method receives the edited record as $row, which is null in the create form. Combine the string notation with arrays when you need Laravel rule objects.

Field keys

The field key is the database column name. Keys are slugged with _ as the separator, so they are also lowercased: Always use snake_case keys. The original camelCase key is not recognised in requests, in getField() or as $row->firstName.

Field parameters

Every parameter which is not an admin parameter is passed to the Laravel validator as a rule, see validation rules. The admin parameters are documented on these pages: Fields can be arranged into groups and tabs, and fields of an existing model can be changed with mutateFields(), see groups and tabs.

Custom field parameters

A parameter which only expands into other parameters is a rule alias (custom_rules, global_rules). A parameter with its own logic is a field mutation: one class which changes every field carrying the parameter, in all models. Use a mutation instead of repeating the same change in mutateFields() of several models, and for every parameter a package adds.
app/Admin/Mutations/Money.php
update($field, $key, $model) changes the field, create($field, $key, $model) returns new fields key => field to add next to it, remove($field, $key, $model) returns true to drop the field. Inside the mutation $this->getModel(), $this->getKey() and $this->fields (all fields of the model) are available. Fields are built once and cached, php artisan admin:cache compiles them in the console and long running processes keep them for the whole worker. A value which depends on the logged administrator or on data of the database belongs into a runtime mutation: it implements Admin\Core\Fields\Mutations\RuntimeMutation and is applied in the administration only, to fields with one of its $attributes, on top of the built fields.
Register mutations in $mutations of a package service provider, or in boot() of the application with Fields::addMutation(Money::class). A mutation is registered once, also when the application boots more times in one process.