The Schema Builder provides methods for creating and modifying your database tables. Using a fluent syntax, you can work with your tables without using any vendor specific SQL.
The **Schema** class is used to create and modify tables. Let's jump right into an example:
#### Creating a simple database table:
Schema::create('users', function($table)
{
$table->increments('id');
});
Let's go over this example. The **create** method tells the Schema builder that this is a new table, so it should be created. In the second argument, we passed a Closure which receives a Table instance. Using this Table object, we can fluently add and drop columns and indexes on the table.
> **Note:** Laravel's "boolean" type maps to a small integer column on all database systems.
#### Example of creating a table and adding columns
Schema::table('users', function($table)
{
$table->create();
$table->increments('id');
$table->string('username');
$table->string('email');
$table->string('phone')->nullable();
$table->text('about');
$table->timestamps();
});
<a name="dropping-columns"></a>
## Dropping Columns
#### Dropping a column from a database table:
$table->drop_column('name');
#### Dropping several columns from a database table:
$table->drop_column(array('name', 'email'));
<a name="adding-indexes"></a>
## Adding Indexes
The Schema builder supports several types of indexes. There are two ways to add the indexes. Each type of index has its method; however, you can also fluently define an index on the same line as a column addition. Let's take a look:
#### Fluently creating a string column with an index:
$table->string('email')->unique();
If defining the indexes on a separate line is more your style, here are example of using each of the index methods:
`$table->unique('email');` | Adding a unique index
`$table->fulltext('description');` | Adding a full-text index
`$table->index('state');` | Adding a basic index
<a name="dropping-indexes"></a>
## Dropping Indexes
To drop indexes you must specify the index's name. Laravel assigns a reasonable name to all indexes. Simply concatenate the table name and the names of the columns in the index, then append the type of the index. Let's take a look at some examples:
Command | Description
------------- | -------------
`$table->drop_primary('users_id_primary');` | Dropping a primary key from the "users" table
`$table->drop_unique('users_email_unique');` | Dropping a unique index from the "users" table
`$table->drop_fulltext('profile_description_fulltext');` | Dropping a full-text index from the "profile" table
`$table->drop_index('geo_state_index');` | Dropping a basic index from the "geo" table
<a name="foreign-keys"></a>
## Foreign Keys
You may easily add foreign key constraints to your table using Schema's fluent interface. For example, let's assume you have a **user_id** on a **posts** table, which references the **id** column of the **users** table. Here's how to add a foreign key constraint for the column:
You may also easily drop a foreign key constraint. The default foreign key names follow the [same convention](#dropping-indexes) as the other indexes created by the Schema builder. Here's an example:
> **Note:** The field referenced in the foreign key is very likely an auto increment and therefore automatically an unsigned integer. Please make sure to create the foreign key field with **unsigned()** as both fields have to be the exact same type, the engine on both tables has to be set to **InnoDB**, and the referenced table must be created **before** the table with the foreign key.