Creating Custom Fields Migrations
To create a new custom fields migration, use the dedicated Artisan command:database/custom-fields/
directory with a timestamp prefix and the name you specified.
Field Types
The system uses string-based field type identifiers. Here are the available field types:Text Types
'text'
- Single line text input'textarea'
- Multi-line text area'rich_editor'
- Rich text editor'markdown_editor'
- Markdown editor
Numeric Types
'number'
- Numeric input'currency'
- Currency formatted input
Date Types
'date'
- Date picker'datetime'
- Date and time picker
Boolean Types
'checkbox'
- Single checkbox'toggle'
- Toggle switch
Choice Types
'select'
- Dropdown selection'radio'
- Radio buttons'multi_select'
- Multiple selection dropdown'checkbox_list'
- Checkbox list'toggle_buttons'
- Toggle button group'tags_input'
- Tag input field
Other Types
'color_picker'
- Color selection'link'
- URL input
Creating Fields
In your migration file, use thenew()
method with CustomFieldData
objects to create new custom fields:
Adding Options to Choice Fields
For choice-based field types, add options using theoptions()
method:
Adding Lookup Types
For fields that reference other models, use thelookupType()
method:
Field Width Options
Control field layout usingCustomFieldWidth
enum:
Updating Fields
To update existing fields, create a new migration and use theupdate()
method:
Deleting Fields
To remove preset fields, create a migration that uses thedelete()
method:
Activating/Deactivating Fields
Control field visibility without deletion:Complete Example
Here’s a comprehensive example creating multiple fields for a Customer model:Running Migrations
Custom fields migrations are run using Laravel’s standard migration system:Best Practices
Naming Conventions
- Use descriptive field names:
'Customer Type'
instead of'Type'
- Use snake_case for codes:
'customer_type'
instead of'CustomerType'
- Use descriptive section names that group related fields
System-Defined Fields
- Set
systemDefined: true
for preset fields - This prevents users from accidentally deleting important fields through the admin interface
Field Organization
- Group related fields in logical sections
- Use consistent section codes across different models when appropriate
- Consider field width to optimize form layout
Error Handling
- Use unique field codes to avoid conflicts
- Test migrations in development before production deployment
- Handle dependencies between fields carefully
Migration Management
- Naming Conventions: Use clear, descriptive names for your migration files
- Versioning: Create separate migrations for creating, updating, and deleting fields to maintain a clear history
- Documentation: Comment your migrations to explain the purpose of each change
- Data Integrity: When updating or deleting fields, consider the impact on existing data