Skip to main content
Category:

How to update Columns in migrations in Laravel

 

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

 

Update 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:

Schema::table('roles', function (Blueprint $table) {
    $table->string('name', 50)->change();
});

We could also modify a column to be nullable:

Schema::table('roles', function (Blueprint $table) {
    $table->string('name', 50)->nullable()->change();
});

 

Rename Columns Attributes

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:

Schema::table('roles', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
});

 

Delete Columns Attributes

To drop a column, use the dropColumn method on the schema builder. 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:

Schema::table('roles', function (Blueprint $table) {
    $table->dropColumn('name');
});

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

Schema::table('roles', function (Blueprint $table) {
    $table->dropColumn(['name', 'title', 'description']);
});

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook