All Migration Column Types in Laravel : Complete Guide

#Laravel #Database #migrations #column type

3 min read.

All Migration Column Types in Laravel : Complete Guide
Hamdaoui Wassim Avatar

Hamdaoui Wassim

0
0

Laravel 12 continues to provide an expressive, fluent API for defining database tables and columns via migrations. Whether you're building a small project or a complex system, understanding the available migration types is crucial for designing clean and efficient databases.

In this article, we’ll cover all column types in Laravel 12 migrations, categorized for easier navigation, with syntax and practical examples.

Primary Key Columns :

Method Description
id() Auto-incrementing UNSIGNED BIGINT (Primary Key)
bigIncrements() Alias for id() (older style)
increments() Auto-incrementing UNSIGNED INT (deprecated in favor of id())
uuid() UUID column (useful for public identifiers)

 

<?php

$table->id();
$table->uuid('uuid')->primary();

?>

Numeric Columns :

Method Description
integer('column') INT
tinyInteger('column') TINYINT
smallInteger('column') SMALLINT
mediumInteger('column') MEDIUMINT
bigInteger('column') BIGINT
unsignedInteger() Unsigned INT
decimal('column', precision, scale) Fixed-point number
float('column', total, places) FLOAT
double() DOUBLE
boolean() BOOLEAN / TINYINT(1)
enum('column', ['x', 'y']) ENUM
json() JSON data
jsonb() JSONB (for PostgreSQL)

 

<?php

$table->integer('views')->unsigned();
$table->decimal('price', 8, 2);
$table->boolean('is_active')->default(true);

?>

 

String & Text Columns :

Method Description
string('column', length) VARCHAR (default 255)
char('column', length) CHAR
text('column') TEXT
mediumText() MEDIUMTEXT
longText() LONGTEXT

 

<?php

$table->string('name');
$table->text('description');


?>

Date & Time Columns :

Method Description
date('column') DATE
dateTime('column') DATETIME
dateTimeTz() DATETIME with timezone
time('column') TIME
timestamp('column') TIMESTAMP
timestampTz() TIMESTAMP with timezone
year('column') YEAR
timestamps() Adds created_at and updated_at
nullableTimestamps() Same as above but nullable
softDeletes() Adds deleted_at for soft deletes
softDeletesTz() Adds deleted_at with timezone
<?php

$table->timestamps();
$table->softDeletes();


?>

Foreign Keys & Relationships :

Method Description
foreignId('user_id') Unsigned BIGINT with optional FK
constrained() Auto-detect table and column for FK
references()->on() Explicit foreign key definition
onDelete('cascade') FK delete behavior
<?php

$table->foreignId('user_id')->constrained()->onDelete('cascade');

?>

Miscellaneous Columns :

 

Method Description
binary('column') BLOB
ipAddress('column') Stores IPv4 or IPv6
macAddress('column') Stores MAC address
uuid('column') UUID string
rememberToken() Adds 100-char nullable remember_token
morphs() Adds *_id and *_type for polymorphic relations
nullableMorphs() Nullable morphs
ulid() ULID-based primary keys
timestampsTz() created_at and updated_at with timezone
<?php

$table->ipAddress('visitor_ip');
$table->morphs('commentable');


?>

 

Column Modifiers : 

You can chain these with any column:

 

Modifier Description
->nullable() Allows NULL values
->default($value) Sets a default
->unique() Unique index
->index() Adds index
->comment('...') Adds comment
->after('column') Adds after another column (MySQL only)
->change('column') Modify existing column
<?php

$table->string('slug')->unique()->nullable();

?>

Example Migration :

<?php

public function up(): void
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->text('description')->nullable();
        $table->decimal('price', 8, 2);
        $table->boolean('is_available')->default(true);
        $table->timestamps();
    });
}


?>

 

Laravel 12 offers a wide range of migration column types and modifiers that allow you to build precise, scalable, and clean database schemas. Keep your migrations version-controlled, lean, and well-commented for long-term maintainability.

0 Comments