Introduction
Database Migrations in Laravel provide a convenient way to interact with the database and help us properly structure our database, so working with the migrations and database is quite simple and easy, but things get a bit tricky if we need to make changes to the already defined migrations, what if we need to make that subscribed_at
column nullable
Working on changes like these would still be simple if we are working in a local environment (in the development phase), we can update the already defined migration file and run php artisan migrate:fresh --seed
and there won't be any issues.
But, what if we are working on an ongoing project and the system is already live, already been used by real-world users, we can't migrate:fresh
the database since all the data will be lost, so we need to implement a way that would not affect the existing database and also alter the database column as per our needs. In this guide, we will be looking at altering the database columns without affecting the existing database.
Prerequisites
Before modifying a column, we must install the doctrine/dbal
package using the Composer package manager. The Doctrine DBAL library is used to determine the current state of the column and to create the SQL queries needed to make the requested changes to our column:
composer require doctrine/dbal
Updating Column Attributes
The change
method allows us to modify the type and attributes of existing columns. For example, if we 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. To accomplish this, we can simply define the new state of the column and then call the change
method.
One thing to note is that we will create new migration files and then call these modifier functions, we won't make any changes to the existing migration files.
// previous migration
Schema::table('users', function (Blueprint $table) {
$table->string('name', 25);
});
// new migration
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->change();
});
And to make an existing column nullable:
// previous migration
Schema::table('users', function (Blueprint $table) {
$table->string('name', 25);
});
// new migration
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->nullable()->change();
});
To see what other columns can be modified, check this link
Renaming Columns
To rename a column, we can use the renameColumn
method provided by the schema builder blueprint like so:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
So, if we want to rename a column from user_name to name, we can define it like so:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('user_name', 'name');
});
Conclusion
As we've seen in the above examples, Laravel comes with all these tiny features and well-thought-out implementations that provide us with an amazing DX(developer experience). Knowing real-world use cases like these is what ultimately helps us become better developers.
And do comment below to give feedback/suggestions about the blog and if you have any requests for a topic that you want us to cover, leave a comment and we'll see if we can cover it for you!