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 required adjustments:
composer require doctrine/dbal
Updating Column Attributes The change
method allows you to modify type and attributes of existing columns. 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:
public function up()
{
Schema::table('test', function (Blueprint $table) {
$table->increments('id');
$table->string('test_name')->change()
;
});
}
We could also modify a column to be nullable:
public function up()
{
Schema::table('test', function (Blueprint $table) {
$table->increments('id');
$table->string('test_name')->nullable()->change();
});
}
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:
public function up()
{
Schema::table('test', function (Blueprint $table) {
$table->renameColumn('test_name', 'Name');
});
}