Laravel Schema Cheat Sheet: Quick Guide to Database Migrations
This Laravel Schema cheat sheet provides concise, English-translated code examples for creating, modifying, and managing database tables, columns, indexes, and foreign keys, covering table creation, column types, indexing options, and common schema operations with clear syntax.
Overview
Laravel’s Schema facade is a helper for defining and manipulating database tables in migrations. The cheat sheet below lists the most frequently used commands, translated into English, to help developers work efficiently with Laravel’s migration system.
Table Creation and Basic Operations
// Create a new table
Schema::create('table', function ($table) {
$table->increments('id');
});
// Use a specific connection
Schema::connection('foo')->create('table', function ($table) {});
// Rename a table
Schema::rename($from, $to);
// Drop a table
Schema::drop('table');
// Drop a table if it exists
Schema::dropIfExists('table');
// Check if a table exists
Schema::hasTable('table');
// Check if a column exists on a table
Schema::hasColumn('table', 'column');
// Modify an existing table
Schema::table('table', function ($table) {});
// Rename a column
$table->renameColumn('from', 'to');
// Drop one or more columns
$table->dropColumn(string|array);
// Set the storage engine (MySQL only)
$table->engine = 'InnoDB';
// Change column order (MySQL only)
$table->string('name')->after('email');Indexes and Keys
// Unique index on a single column
$table->string('column')->unique();
// Primary key on a single column
$table->primary('column');
// Composite primary key
$table->primary(array('first', 'last'));
// Simple unique index
$table->unique('column');
$table->unique('column', 'key_name');
// Composite unique index
$table->unique(array('first', 'last'));
$table->unique(array('first', 'last'), 'key_name');
// Simple index
$table->index('column');
$table->index('column', 'key_name');
// Composite index
$table->index(array('first', 'last'));
$table->index(array('first', 'last'), 'key_name');
// Drop primary key
$table->dropPrimary(array('column'));
$table->dropPrimary('table_column_primary');
// Drop unique index
$table->dropUnique(array('column'));
$table->dropUnique('table_column_unique');
// Drop index
$table->dropIndex(array('column'));
$table->dropIndex('table_column_index');
// Foreign key definition
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'|'restrict'|'set null'|'no action');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade'|'restrict'|'set null'|'no action');
// Drop foreign key
$table->dropForeign(array('user_id'));
$table->dropForeign('posts_user_id_foreign');Column Types
// Auto‑incrementing IDs
$table->increments('id');
$table->bigIncrements('id');
// Integer types
$table->integer('votes');
$table->tinyInteger('votes');
$table->smallInteger('votes');
$table->mediumInteger('votes');
$table->bigInteger('votes');
// Floating‑point numbers
$table->float('amount');
$table->double('column', 15, 8);
$table->decimal('amount', 5, 2);
// String and text types
$table->char('name', 4);
$table->string('email');
$table->string('name', 100);
$table->text('description');
$table->mediumText('description');
$table->longText('description');
// Date and time types
$table->date('created_at');
$table->dateTime('created_at');
$table->time('sunrise');
$table->timestamp('added_on');
// Adds created_at and updated_at columns
$table->timestamps();
$table->nullableTimestamps();
// Miscellaneous types
$table->binary('data');
$table->boolean('confirmed');
$table->softDeletes(); // adds deleted_at for soft deletes
$table->enum('choices', array('foo', 'bar'));
$table->rememberToken(); // adds remember_token column
$table->morphs('parent'); // adds parent_id and parent_type columns
// Column modifiers (chained after column definition)
->nullable()
->default($value)
->unsigned()
->comment('Your comment');This reference covers the essential Schema commands needed to define tables, columns, indexes, foreign keys, and common column modifiers in Laravel migrations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
