Home » Laravel » How to Change Column in Migration Laravel

How to Change Column in Migration Laravel

Laravel allows developers to use migrations for creating database tables. However, it’s not always easy to determine in advance which columns will be needed in the future or what type of data they will store. Consequently, rarely you may want to change a column name or even its data type.

You can use the renameColumn() method to rename columns, and the change() method to update their properties. In this article, I will show how to change column in Laravel migration in detail.

Table of Contents

Modifying Columns in Laravel Migrations

If you use Laravel 9 or an older version, you will need to install the doctrine/dbal package to be able to rename or modify columns using migrations:

composer require doctrine/dbal

But if you are using Laravel 10 or a newer version, you do not need this package anymore. Now Laravel supports renaming and changing columns natively.

I will use the table with name articles for all examples in this article. You can create it by using this migration:

php artisan make:migration CreateArticlesTable Schema::create("articles", function (Blueprint $table) { $table->id(); $table->string("title"); $table->string("text"); $table->string("author"); $table->integer("status"); $table->timestamps(); });

How to Rename Column

Renaming columns is a straightforward process. Suppose you have a table named articles and want to rename the text column to content. In that case, you can use the following code snippet in your migration file:

public function up(): void { Schema::table("articles", function (Blueprint $table) { $table->renameColumn("text", "content"); }); }

After running this migration, your column will be renamed as content:

Also, add the down() method with rollback renaming to make migration reversible:

public function down(): void { Schema::table("articles", function (Blueprint $table) { $table->renameColumn("content", "text"); }); }

How to Change Column

Laravel allows to modify not only column type but also its properties, such as nullable state, default value, comment and etc. To change the column type in Laravel, just use a new type and call the change() method at the end of the chain. For example, if you want to change the status field’s type from integer to string, use this code:

Schema::table("articles", function (Blueprint $table) { $table->string("status")->change(); });

In the same way, you can change the nullable state:

Schema::table("articles", function (Blueprint $table) { $table ->string("status") ->nullable() ->change(); }); //Or make column not nullable Schema::table("articles", function (Blueprint $table) { $table ->string("status") ->nullable(false) ->change(); });

Or a default value:

Schema::table("articles", function (Blueprint $table) { $table ->string("status") ->default("pending") ->change(); });

In addition, you can do all of this in on shoot:

Schema::table("articles", function (Blueprint $table) { $table ->string("status") ->nullable(false) ->default("pending") ->change(); });

Note, that both doctribe/dbal package and Laravel’s native implementation of column changing have limitations for certain database engines. For example, in PostgreSQL and SQL Server, you can change properties for enum data types. So, if you are using PostgreSQL, consider using the string type and controlling its values on the application level instead of the enum type.

The modifiers that are described above are supported by most database engines. However, not all additional changing operations are supported by different database engines. Here are the most interesting operations that are supported by MySQL or MariaDB:

  • after($columnName) – place the column after the specified column;
  • first() – place the column at the first position in the table;
  • autoIncrement() – enable auto-incrementing column for each next record;
  • from($number) – set the start value for auto incrementing;
  • unsigned() – make integer column unsigned;
  • useCurrent() – enable using CURRENT_TIMESTAMP value for timestamp columns;
  • useCurrentOnUpdate() – the same as the previous, but the timestamp updates on each record update;
  • renameTo($newName) – allows to rename the column.
  • storedAs($expression) – specify an expression that is used to calculate the value for the column and store it in the database;
  • virtualAs($expression) – the same as the previous but is calculated on the fly;

PostgreSQL only supports a limited number of modifiers from the list mentioned above, such as useCurrent(), autoIncrement(), from(), and storedAs(). You can find more information about them and other modifiers in the official Laravel documentation.

Wrapping Up

In this short article, I have explained how to change column in migration Laravel. As you can see it may be very useful because you can change type or column properties without deleting them and losing data.

Leave a Comment

Exit mobile version