Database: Migrations

Introduction

Migrations are like version control for your database, allowing a team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.

The Laravel Schema facade provides database agnostic support for creating and manipulating tables. It shares the same expressive, fluent API across all of Laravel's supported database systems.

Generating Migrations

To create a migration, use the make:migration Artisan command:

  1. php artisan make:migration create_users_table

The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.

The —table and —create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options simply pre-fill the generated migration stub file with the specified table:

  1. php artisan make:migration add_votes_to_users_table --table=users
  2. php artisan make:migration create_users_table --create=users

If you would like to specify a custom output path for the generated migration, you may use the —path option when executing the make:migration command. The provided path should be relative to your application's base path.

Migration Structure

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should simply reverse the operations performed by the up method.

Within both of these methods you may use the Laravel schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder, check out its documentation. For example, let's look at a sample migration that creates a flights table:

  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class CreateFlightsTable extends Migration
  5. {
  6. /**
  7. * Run the migrations.
  8. *
  9. * @return void
  10. */
  11. public function up()
  12. {
  13. Schema::create('flights', function (Blueprint $table) {
  14. $table->increments('id');
  15. $table->string('name');
  16. $table->string('airline');
  17. $table->timestamps();
  18. });
  19. }
  20. /**
  21. * Reverse the migrations.
  22. *
  23. * @return void
  24. */
  25. public function down()
  26. {
  27. Schema::drop('flights');
  28. }
  29. }

Running Migrations

To run all outstanding migrations for your application, use the migrate Artisan command. If you are using the Homestead virtual machine, you should run this command from within your VM:

  1. php artisan migrate

If you receive a "class not found" error when running migrations, try running the composer dump-autoload command and re-issuing the migrate command.

Forcing Migrations To Run In Production

Some migration operations are destructive, meaning they may cause you to lose data. In order to protect you from running these commands against your production database, you will be prompted for confirmation before these commands are executed. To force the commands to run without a prompt, use the —force flag:

  1. php artisan migrate --force

Rolling Back Migrations

To rollback the latest migration "operation", you may use the rollback command. Note that this rolls back the last "batch" of migrations that ran, which may include multiple migration files:

  1. php artisan migrate:rollback

The migrate:reset command will roll back all of your application's migrations:

  1. php artisan migrate:reset

Rollback / Migrate In Single Command

The migrate:refresh command will first roll back all of your database migrations, and then run the migrate command. This command effectively re-creates your entire database:

  1. php artisan migrate:refresh
  2. php artisan migrate:refresh --seed

Writing Migrations

Creating Tables

To create a new database table, use the create method on the Schema facade. The create method accepts two arguments. The first is the name of the table, while the second is a Closure which receives a Blueprint object used to define the new table:

  1. Schema::create('users', function (Blueprint $table) {
  2. $table->increments('id');
  3. });

Of course, when creating the table, you may use any of the schema builder's column methods to define the table's columns.

Checking For Table / Column Existence

You may easily check for the existence of a table or column using the hasTable and hasColumn methods:

  1. if (Schema::hasTable('users')) {
  2. //
  3. }
  4. if (Schema::hasColumn('users', 'email')) {
  5. //
  6. }

Connection & Storage Engine

If you want to perform a schema operation on a database connection that is not your default connection, use the connection method:

  1. Schema::connection('foo')->create('users', function ($table) {
  2. $table->increments('id');
  3. });

To set the storage engine for a table, set the engine property on the schema builder:

  1. Schema::create('users', function ($table) {
  2. $table->engine = 'InnoDB';
  3. $table->increments('id');
  4. });

Renaming / Dropping Tables

To rename an existing database table, use the rename method:

  1. Schema::rename($from, $to);

To drop an existing table, you may use the drop or dropIfExists methods:

  1. Schema::drop('users');
  2. Schema::dropIfExists('users');

Creating Columns

To update an existing table, we will use the table method on the Schema facade. Like the create method, the table method accepts two arguments: the name of the table and a Closure that receives a Blueprint instance we can use to add columns to the table:

  1. Schema::table('users', function ($table) {
  2. $table->string('email');
  3. });

Available Column Types

Of course, the schema builder contains a variety of column types that you may use when building your tables:

CommandDescription
$table->bigIncrements('id');Incrementing ID (primary key) using a "UNSIGNED BIG INTEGER" equivalent.
$table->bigInteger('votes');BIGINT equivalent for the database.
$table->binary('data');BLOB equivalent for the database.
$table->boolean('confirmed');BOOLEAN equivalent for the database.
$table->char('name', 4);CHAR equivalent with a length.
$table->date('created_at');DATE equivalent for the database.
$table->dateTime('created_at');DATETIME equivalent for the database.
$table->decimal('amount', 5, 2);DECIMAL equivalent with a precision and scale.
$table->double('column', 15, 8);DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.
$table->enum('choices', ['foo', 'bar']);ENUM equivalent for the database.
$table->float('amount');FLOAT equivalent for the database.
$table->increments('id');Incrementing ID (primary key) using a "UNSIGNED INTEGER" equivalent.
$table->integer('votes');INTEGER equivalent for the database.
$table->json('options');JSON equivalent for the database.
$table->jsonb('options');JSONB equivalent for the database.
$table->longText('description');LONGTEXT equivalent for the database.
$table->mediumInteger('numbers');MEDIUMINT equivalent for the database.
$table->mediumText('description');MEDIUMTEXT equivalent for the database.
$table->morphs('taggable');Adds INTEGER taggable_id and STRING taggable_type.
$table->nullableTimestamps();Same as timestamps(), except allows NULLs.
$table->rememberToken();Adds remember_token as VARCHAR(100) NULL.
$table->smallInteger('votes');SMALLINT equivalent for the database.
$table->softDeletes();Adds deleted_at column for soft deletes.
$table->string('email');VARCHAR equivalent column.
$table->string('name', 100);VARCHAR equivalent with a length.
$table->text('description');TEXT equivalent for the database.
$table->time('sunrise');TIME equivalent for the database.
$table->tinyInteger('numbers');TINYINT equivalent for the database.
$table->timestamp('added_on');TIMESTAMP equivalent for the database.
$table->timestamps();Adds created_at and updated_at columns.
$table->uuid('id');UUID equivalent for the database.

Column Modifiers

In addition to the column types listed above, there are several other column "modifiers" which you may use while adding the column. For example, to make the column "nullable", you may use the nullable method:

  1. Schema::table('users', function ($table) {
  2. $table->string('email')->nullable();
  3. });

Below is a list of all the available column modifiers. This list does not include the index modifiers:

ModifierDescription
->first()Place the column "first" in the table (MySQL Only)
->after('column')Place the column "after" another column (MySQL Only)
->nullable()Allow NULL values to be inserted into the column
->default($value)Specify a "default" value for the column
->unsigned()Set integer columns to UNSIGNED

Modifying Columns

Prerequisites

Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column.

Updating Column Attributes

The change method allows you to modify an existing column to a new type, or modify the column's attributes. For example, you may wish to increase the size of a string column. To see the change method in action, let's increase the size of the name column from 25 to 50:

  1. Schema::table('users', function ($table) {
  2. $table->string('name', 50)->change();
  3. });

We could also modify a column to be nullable:

  1. Schema::table('users', function ($table) {
  2. $table->string('name', 50)->nullable()->change();
  3. });

Renaming Columns

To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file:

  1. Schema::table('users', function ($table) {
  2. $table->renameColumn('from', 'to');
  3. });

Note: Renaming columns in a table with a enum column is not currently supported.

Dropping Columns

To drop a column, use the dropColumn method on the Schema builder:

  1. Schema::table('users', function ($table) {
  2. $table->dropColumn('votes');
  3. });

You may drop multiple columns from a table by passing an array of column names to the dropColumn method:

  1. Schema::table('users', function ($table) {
  2. $table->dropColumn(['votes', 'avatar', 'location']);
  3. });

Note: Before dropping columns from a SQLite database, you will need to add the doctrine/dbal dependency to your composer.json file and run the composer update command in your terminal to install the library.

Note: Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported.

Creating Indexes

The schema builder supports several types of indexes. First, let's look at an example that specifies a column's values should be unique. To create the index, we can simply chain the unique method onto the column definition:

  1. $table->string('email')->unique();

Alternatively, you may create the index after defining the column. For example:

  1. $table->unique('email');

You may even pass an array of columns to an index method to create a compound index:

  1. $table->index(['account_id', 'created_at']);

Available Index Types

CommandDescription
$table->primary('id');Add a primary key.
$table->primary(['first', 'last']);Add composite keys.
$table->unique('email');Add a unique index.
$table->index('state');Add a basic index.

Dropping Indexes

To drop an index, you must specify the index's name. By default, Laravel automatically assigns a reasonable name to the indexes. Simply concatenate the table name, the name of the indexed column, and the index type. Here are some examples:

CommandDescription
$table->dropPrimary('users_id_primary');Drop a primary key from the "users" table.
$table->dropUnique('users_email_unique');Drop a unique index from the "users" table.
$table->dropIndex('geo_state_index');Drop a basic index from the "geo" table.

If you pass an array of columns into a method that drops indexes, the conventional index name will be generated based on the table name, columns and key type.

  1. Schema::table('geo', function ($table) {
  2. $table->dropIndex(['state']); // Drops index 'geo_state_index'
  3. });

Foreign Key Constraints

Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level. For example, let's define a user_id column on the posts table that references the id column on a users table:

  1. Schema::table('posts', function ($table) {
  2. $table->integer('user_id')->unsigned();
  3. $table->foreign('user_id')->references('id')->on('users');
  4. });

You may also specify the desired action for the "on delete" and "on update" properties of the constraint:

  1. $table->foreign('user_id')
  2. ->references('id')->on('users')
  3. ->onDelete('cascade');

To drop a foreign key, you may use the dropForeign method. Foreign key constraints use the same naming convention as indexes. So, we will concatenate the table name and the columns in the constraint then suffix the name with "_foreign":

  1. $table->dropForeign('posts_user_id_foreign');

Or you may pass an array value which will automatically use the conventional constraint name when dropping:

  1. $table->dropForeign(['user_id']);