Field Types
Fields are an integral part of CRUD Templates. They are fed into a template in one of two ways:
The --fields option where you can manually specify the fields and their types, or the --table option, which automatically generates fields from an existing database table.
These fields are then used to intelligently populate the content of the generated files.
Basic Syntax
Fields are defined using the following format:
fieldName:fieldTypeFor nullable fields, add a ? after the field name:
fieldName?:fieldTypeMultiple fields are separated by commas:
--fields="name:string,age:integer,bio?:text"Supported Field Types
String
Creates a varchar column with a maximum of 255 characters.
Syntax:
--fields="name:string"Migration:
$table->string('name');Validation:
'name' => ['required', 'string', 'max:255']Model Cast: None (default string handling)
Example:
php artisan crud:generate User --fields="first_name:string,last_name:string,email:string"Integer
Creates an integer column for whole numbers.
Syntax:
--fields="age:integer"Migration:
$table->integer('age');Validation:
'age' => ['required', 'integer']Model Cast: None (default integer handling)
Example:
php artisan crud:generate Product --fields="stock:integer,views:integer"Decimal
Creates a decimal column for precise numeric values (e.g., prices, measurements).
Syntax:
--fields="price:decimal"Migration:
$table->decimal('price', 8, 2);Validation:
'price' => ['required', 'numeric']Model Cast:
'price' => 'decimal:2'Example:
php artisan crud:generate Product --fields="price:decimal,weight:decimal"Date
Creates a date column (without time).
Syntax:
--fields="birth_date:date"Migration:
$table->date('birth_date');Validation:
'birth_date' => ['required', 'date']Model Cast:
'birth_date' => 'immutable_date'Example:
php artisan crud:generate Employee --fields="birth_date:date,hire_date:date"DateTime
Creates a datetime column with both date and time.
Syntax:
--fields="published_at:datetime"Migration:
$table->dateTime('published_at');Validation:
'published_at' => ['required', 'date']Model Cast:
'published_at' => 'immutable_datetime'Example:
php artisan crud:generate Post --fields="published_at:datetime,scheduled_at?:datetime"Text
Creates a text column for longer content (no length limit).
Syntax:
--fields="description:text"Migration:
$table->text('description');Validation:
'description' => ['required', 'string']Model Cast: None
Example:
php artisan crud:generate Article --fields="content:text,excerpt?:text"Boolean
Creates a boolean column for true/false values.
Syntax:
--fields="is_active:boolean"Migration:
$table->boolean('is_active');Validation:
'is_active' => ['required', 'boolean']Model Cast:
'is_active' => 'boolean'Example:
php artisan crud:generate User --fields="is_active:boolean,is_verified:boolean"Enum
Creates a string column with enum casting (requires PHP enum class).
Syntax:
--fields="status:enum:StatusEnum"Migration:
$table->string('status');Validation:
'status' => ['required', Rule::enum(StatusEnum::class)]Model Cast:
'status' => StatusEnum::classExample:
php artisan crud:generate Order --fields="status:enum:OrderStatus,priority:enum:Priority"Enum Requirements
You must create the enum class before generating the CRUD. The enum class should exist in the App\Enums namespace.
JSON
Creates a JSON column for storing structured data.
Syntax:
--fields="metadata:json"Migration:
$table->json('metadata');Validation:
'metadata' => ['required', 'array']Model Cast:
'metadata' => 'array'Example:
php artisan crud:generate Product --fields="attributes:json,settings?:json"Nullable Fields
Add a ? after the field name to make it nullable:
php artisan crud:generate User --fields="name:string,phone?:string,bio?:text"This will:
- Make the column
->nullable()in the migration - Replace
requiredwithnullablein validation rules
Complete Example
Here's a comprehensive example using multiple field types:
php artisan crud:generate Product \
--fields="name:string,sku:string,description:text,price:decimal,stock:integer,is_active:boolean,published_at?:datetime,metadata?:json"This generates a product CRUD with:
- String fields for name and SKU
- Text field for description
- Decimal field for price
- Integer field for stock quantity
- Boolean field for active status
- Optional datetime for publication date
- Optional JSON field for additional metadata
Next Steps
- Learn about Relationships to connect models
- Explore Generate from Schema for existing tables
- Understand how to create Custom Field Types