Schema Builder

The Laravel class provides a database agnostic way of manipulating tables. It works well with all of the databases supported by Laravel, and has a unified API across all of these systems.

Creating & Dropping Tables

To create a new database table, the Schema::create method is used:

The first argument passed to the create method is the name of the table, and the second is a Closure which will receive a Blueprint object which may be used to define the new table.

To rename an existing database table, the rename method may be used:

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

To specify which connection the schema operation should take place on, use the Schema::connection method:

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

To drop a table, you may use the Schema::drop method:

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

Adding Columns

To update an existing table, we will use the Schema::table method:

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

The table builder contains a variety of column types that you may use when building your tables:

Using After On MySQL

If you are using the MySQL database, you may use the after method to specify the order of columns:

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

Changing Columns

Note: Before changing a column, be sure to add the doctrine/dbal dependency to your composer.json file.

Sometimes you may need to modify an existing column. For example, you may wish to increase the size of a string column. The change method makes it easy! For example, let's increase the size of the name column from 25 to 50:

We could also modify a column to be nullable:

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

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. {
  3. $table->renameColumn('from', 'to');

Dropping Columns

To drop a column, you may use the dropColumn method on the Schema builder. Before dropping a column, be sure to add the doctrine/dbal dependency to your composer.json file.

Dropping A Column From A Database Table

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

Dropping Multiple Columns From A Database Table

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

Checking Existence

Checking For Existence Of Table

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. }

Checking For Existence Of Columns

Adding Indexes

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

Or, you may choose to add the indexes on separate lines. Below is a list of all available index types:

Laravel also provides support for adding foreign key constraints to your tables:

  1. $table->integer('user_id')->unsigned();
  2. $table->foreign('user_id')->references('id')->on('users');

In this example, we are stating that the user_id column references the id column on the users table. Make sure to create the foreign key column first!

You may also specify options for the "on delete" and "on update" actions 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. A similar naming convention is used for foreign keys as is used for other indexes:

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

Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.

Dropping Indexes

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

Dropping Timestamps & SoftDeletes

To drop the timestamps, nullableTimestamps or softDeletes column types, you may use the following methods:

Storage Engines

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

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