Schema::create(), table(), drop(), dropIfExists()
Beginner5 min read·lv-14-003
sql
Concept
Laravel's Blueprint provides methods for every SQL column type, mapped to database-appropriate types across MySQL, PostgreSQL, and SQLite.
String and text:
string(string $column, int $length = 255):VARCHAR(n).char(string $column, int $length = 255): Fixed-lengthCHAR(n).text(string $column):TEXT(65KB).mediumText/longText: MySQLMEDIUMTEXT(16MB) /LONGTEXT(4GB).tinyText: MySQLTINYTEXT(255 bytes).
Numeric:
integer($col)/tinyInteger/smallInteger/mediumInteger/bigInteger: Signed integers.unsignedInteger/unsignedBigInteger: Unsigned variants.decimal($col, int $precision = 8, int $scale = 2): Fixed-point decimal. Use for money — neverfloat.float($col, int $precision = 53): Floating point. Imprecise — avoid for financial values.double($col): Double precision float.
Boolean and binary:
boolean($col):TINYINT(1).binary($col):BLOB.
Date and time:
date($col):DATE.dateTime($col, int $precision = 0):DATETIMEwith optional microsecond precision.timestamp($col, int $precision = 0):TIMESTAMP.timestamps(int $precision = 0): Addscreated_atandupdated_atas nullable timestamps.softDeletes(): Nullabledeleted_attimestamp.year($col):YEAR.time($col):TIME.
Special:
json($col):JSON(MySQL/PostgreSQL native JSON).uuid($col):CHAR(36)orUUIDdepending on driver.ulid($col):CHAR(26).ipAddress($col):VARCHAR(45).macAddress($col):VARCHAR(17).enum($col, array $allowed):ENUM(...)— MySQL only.set($col, array $allowed):SET(...)— MySQL only.
Code Example
php
<?php
Schema::create('users', function (Blueprint $table) {
// Primary key variants
$table->id(); // BIGINT UNSIGNED AUTO_INCREMENT PK (shorthand for $table->bigIncrements('id'))
// $table->uuid('id')->primary(); // UUID primary key
// $table->ulid('id')->primary(); // ULID primary key
// String types
$table->string('name', 100); // VARCHAR(100)
$table->string('email')->unique(); // VARCHAR(255) with unique index
$table->char('country_code', 2); // CHAR(2)
$table->text('bio')->nullable(); // TEXT
// Numeric types
$table->unsignedSmallInteger('age')->nullable();
$table->decimal('balance', 12, 2)->default(0.00); // use for money
$table->unsignedInteger('login_count')->default(0);
// Boolean
$table->boolean('is_active')->default(true);
$table->boolean('email_verified')->default(false);
// Date/time
$table->date('date_of_birth')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->timestamps(); // created_at + updated_at
$table->softDeletes(); // deleted_at
// JSON
$table->json('preferences')->nullable();
$table->json('metadata')->nullable();
// Enum (MySQL)
$table->enum('role', ['admin', 'editor', 'user'])->default('user');
// UUID for related models
$table->uuid('external_id')->nullable()->unique();
});
// Column modifiers (chainable)
$table->string('phone')->nullable()->after('email');
$table->integer('sort_order')->default(0)->unsigned()->comment('Display order');
$table->string('slug')->unique()->index()->after('title');