Jump to: navigation, search

Migrations

Contents

Laravel

Way Generators

Tips

Always use the generators to create your migrations.

  1. php artisan generate:migration create_users_table


When changing an existing migration, create a new one

  1. php artisan generate:migration add_display_name_to_users


Standard Schema

Commands
Syntax What it does
$table->increments('id'); Creates an auto incrementing primary key
$table->string('name', 100); Creates a VARCHAR column (Length optional)
$table->text('description'); Creates a TEXT column
$table->integer('votes', 1, 2); Creates an INTEGER column (Decimal and Precision optional)
$table->boolean('confirmed'); Creates a TINYINT(1) column
$table->date('created_at'); Creates a DATE column
$table->timestamps(); Adds the created_at and updated_at columns
$table->softDeletes(); Adds the deleted_at column
$table->string('name')->nullable(); Designates that the column allows NULL values
$table->string('name')->default('Stygian'); Sets the default value for the column
$table->string('name')->after('email'); Places the column after the desired column
$table->renameColumn('from', 'to'); Renames a column
$table->dropColumn('votes'); Drops a column from the table
Indexes
Index What it does
$table->string('name')->index(); Sets the column as an index
$table->string('name')->unique(); Sets the column as a unique index
$table->primary('id'); Sets the column as a primary key
You can use an array instead of a single column to create composite keys