

To drop a foreign key, you may use the dropForeign method. You may also specify options for the "on delete" and "on update" actions of the constraint: $table->foreign('user_id') Make sure to create the foreign key column first! In this example, we are stating that the user_id column references the id column on the users table. Laravel also provides support for adding foreign key constraints to your tables: $table->integer('user_id')->unsigned() Below is a list of all available index types: Command Or, you may choose to add the indexes on separate lines. First, you may fluently define them on a column definition, or you may add them separately: $table->string('email')->unique()

The schema builder supports several types of indexes. } Checking For Existence Of Columns if (Schema::hasColumn('users', 'email')) You may easily check for the existence of a table or column using the hasTable and hasColumn methods: if (Schema::hasTable('users')) $table->dropColumn() Ĭhecking Existence Checking For Existence Of Table }) Dropping Multiple Columns From A Database Table Schema::table('users', function($table) Dropping A Column From A Database Table Schema::table('users', function($table) Before dropping a column, be sure to add the doctrine/dbal dependency to your composer.json file. To drop a column, you may use the dropColumn method on the Schema builder. Note: Renaming columns in a table with enum column is currently not supported. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file. To rename a column, you may use the renameColumn method on the Schema builder. We could also modify a column to be nullable: Schema::table('users', function($table) The change method makes it easy! For example, let's increase the size of the name column from 25 to 50: Schema::table('users', function($table) For example, you may wish to increase the size of a string column. Sometimes you may need to modify an existing column. Note: Before changing a column, be sure to add the doctrine/dbal dependency to your composer.json file. If you are using the MySQL database, you may use the after method to specify the order of columns: $table->string('name')->after('email') Same as timestamps(), except allows NULLsĭesignate that the column allows NULL values Incrementing ID to the table (primary key)Īdds INTEGER taggable_id and STRING taggable_type Incrementing ID using a "big integer" equivalentĭECIMAL equivalent with a precision and scaleĭOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point The table builder contains a variety of column types that you may use when building your tables: Command
DBSCHEMA SET PRIMARY KEY UPDATE
To update an existing table, we will use the Schema::table method: Schema::table('users', function($table) To drop a table, you may use the Schema::drop method: Schema::drop('users') To specify which connection the schema operation should take place on, use the Schema::connection method: Schema::connection('foo')->create('users', function($table) To rename an existing database table, the rename method may be used: Schema::rename($from, $to) 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 create a new database table, the Schema::create method is used: Schema::create('users', function($table) It works well with all of the databases supported by Laravel, and has a unified API across all of these systems. The Laravel Schema class provides a database agnostic way of manipulating tables.
