Skip to content

Field Definitions

The getFields() method is the core of the AutoCrud trait. It defines the metadata for each model attribute, controlling how fields appear in forms, tables, and API responses.

protected static function getFields(): array
{
return [
[
'name' => 'Field Label',
'field' => 'column_name',
'type' => 'field_type',
'table' => true,
'form' => true,
'rules' => ['required' => true],
],
// More fields...
];
}
PropertyTypeRequiredDescription
namestringYesHuman-readable label for the field
fieldstringYesDatabase column/attribute name
typestringYesField type (controls rendering and casting)
tableboolNoWhether field appears in data tables
formboolNoWhether field appears in forms
rulesarrayNoValidation rules
defaultmixedNoDefault value
onlyUpdateboolNoOnly visible/usable during updates
hiddenboolNoHide in forms (useful for comboField)
optionsarrayNoOptions for select fields
endPointstringNoAPI endpoint for combobox fields
itemTitlestringNoField displayed in combobox
comboFieldstringNoAuxiliary field added as hidden to form
// String field
[
'name' => 'Name',
'field' => 'name',
'type' => 'string',
'table' => true,
'form' => true,
]
// Number field
[
'name' => 'Price',
'field' => 'price',
'type' => 'number',
'table' => true,
'form' => true,
]
// Boolean field
[
'name' => 'Active',
'field' => 'is_active',
'type' => 'boolean',
'table' => true,
'form' => true,
]
// Password field
[
'name' => 'Password',
'field' => 'password',
'type' => 'password',
'form' => true,
'rules' => ['required' => true],
]
// Date field
[
'name' => 'Birth Date',
'field' => 'birth_date',
'type' => 'date',
'table' => true,
'form' => true,
]
// DateTime field
[
'name' => 'Created At',
'field' => 'created_at',
'type' => 'datetime',
'table' => true,
]
// Select field with options
[
'name' => 'Gender',
'field' => 'gender',
'type' => 'select',
'options' => ['Masculino', 'Femenino', 'Otro'],
'table' => true,
'form' => true,
]
// Combobox field
[
'name' => 'Category',
'field' => 'category_id',
'type' => 'combobox',
'endPoint' => '/api/categories',
'itemTitle' => 'name',
'table' => true,
'form' => true,
]
// Text area
[
'name' => 'Description',
'field' => 'description',
'type' => 'text',
'form' => true,
]
// Telephone field
[
'name' => 'Phone',
'field' => 'phone',
'type' => 'telephone',
'table' => true,
'form' => true,
]
// Decimal field
[
'name' => 'Amount',
'field' => 'amount',
'type' => 'decimal',
'table' => true,
'form' => true,
]

Image fields handle file uploads with automatic storage management. Images can be public or private (requires authentication).

// Public image (accessible without auth)
[
'name' => 'Photo',
'field' => 'photo',
'type' => 'image',
'table' => true,
'form' => true,
'public' => true,
]
// Private image (requires authentication)
[
'name' => 'Document Photo',
'field' => 'document_photo',
'type' => 'image',
'table' => true,
'form' => true,
'public' => false,
]
PropertyTypeDefaultDescription
publicbooltrueIf true, image is publicly accessible. If false, requires authentication

Storage paths:

  • Public: storage/public/images/{model}/{field}/{id}
  • Private: storage/private/images/{model}/{field}/{id}

File fields handle general file uploads with optional encryption for private files.

// Public file
[
'name' => 'Attachment',
'field' => 'attachment',
'type' => 'file',
'form' => true,
'public' => true,
]
// Private file (encrypted storage, requires auth)
[
'name' => 'Contract',
'field' => 'contract',
'type' => 'file',
'form' => true,
'public' => false,
]
PropertyTypeDefaultDescription
publicbooltrueIf true, file is publicly accessible. If false, file is encrypted and requires authentication

Storage paths:

  • Public: storage/public/files/{model}/{field}/{id}
  • Private: storage/private/files/{model}/{field}/{id} (encrypted)
[
'name' => 'Email',
'field' => 'email',
'type' => 'string',
'rules' => [
'required' => true,
'unique' => true,
'custom' => ['email_validation'], // References getCustomRules()
],
'table' => true,
'form' => true,
]

The initializeAutoCrud() method automatically applies casting based on field types:

  • number with relation → integer
  • number without relation → string
  • booleanboolean
  • passwordhashed (also added to $hidden)
  • dateDateWithUserTimezone::class . ':d-m-Y'
  • datetimeDateTimeWithUserTimezone::class . ':d-m-Y H:i'
  • telephonestring
  1. Always define required properties: name, field, and type are mandatory
  2. Use descriptive names: Make field labels user-friendly
  3. Set appropriate defaults: Use table and form properties to control visibility
  4. Validate input: Always add appropriate validation rules
  5. Consider relationships: Use proper field types for foreign keys
protected static function getFields(): array
{
return [
[
'name' => 'Name',
'field' => 'name',
'type' => 'string',
'table' => true,
'form' => true,
'rules' => ['required' => true, 'unique' => true],
],
[
'name' => 'Description',
'field' => 'description',
'type' => 'text',
'form' => true,
],
[
'name' => 'Price',
'field' => 'price',
'type' => 'decimal',
'table' => true,
'form' => true,
'rules' => ['required' => true],
],
[
'name' => 'Active',
'field' => 'is_active',
'type' => 'boolean',
'table' => true,
'form' => true,
'default' => true,
],
[
'name' => 'Category',
'field' => 'category_id',
'type' => 'combobox',
'endPoint' => '/api/categories',
'itemTitle' => 'name',
'table' => true,
'form' => true,
'rules' => ['required' => true],
],
];
}