Columns
To define columns, you can use the column
method on the Schema
instance.
This method requires the column name as the first argument. You can then chain additional methods to define the column's properties.
int()
Defines an integer column, typically used for IDs or counters.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'id' )->int()->unsigned()->auto_increment();
});
varchar()
Defines a variable-length character string column, ideal for names or short texts.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'name' )->varchar( 255 );
});
text()
Defines a column for storing large amounts of text data, suitable for long descriptions or content.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'description' )->text();
});
datetime()
Defines a column that stores both date and time information.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'created_at' )->datetime()->default( 'CURRENT_TIMESTAMP', true );
});
boolean()
Defines a column for storing true/false values.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'is_active' )->boolean()->default( true );
});
tinyint()
Defines a column for storing small integer values, often used as an alternative to boolean.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'status' )->tinyint()->unsigned()->default( 1 );
});
bigint()
Defines a column for storing very large integer values.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'user_id' )->bigint()->unsigned();
});
float()
Defines a column for storing approximate numeric data with floating decimal points.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'price' )->float( 8, 2 )->unsigned();
});
decimal()
Defines a column for storing exact numeric data with fixed decimal points, often used for financial calculations.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'precise_amount' )->decimal( 10, 4 )->unsigned();
});
date()
Defines a column for storing date values without time information.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'birth_date' )->date();
});
time()
Defines a column for storing time values without date information.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'shift_start' )->time();
});
timestamp()
Defines a column for storing both date and time, often used for automatically tracking record changes.
Example:
$schema = new Schema( 'my_table', function ( Schema $table ) {
$table->column( 'updated_at' )->timestamp()->default( 'CURRENT_TIMESTAMP', true );
});