Altering Table Through Migrations Laravel

It is a very difficult job to change the column name or type once it is migrated and has contents. In order to modify the column, we need to create another migration where we state the modifications. Let us create a migration first by using the following command

Php artisan make:migration change_post_description_to_nullable_in_tbl_posts —table=tbl_posts

This will create a new migration for the table.

public function up()

{

    Schema::table(tbl_posts, function (Blueprint $table) {

        $table->text(post_description)->nullable()->change();

    });

}

public function down()

{

    Schema::table(‘reviews’, function (Blueprint $table) {

        $table->text(‘body’)->nullable(false)->change();

    });

}

And then we run the command

Php artisan migrate

In order to change the name of the column from post_description to post_information

public function up()

{

    Schema::table(tbl_posts, function (Blueprint $table) {

        $table->renameColumn(‘post_description’, ‘post_information’);

    });

}

Generally, it goes like this,

 $table->renameColumn(‘from’, ‘to’);

I hope this helps you in some way, here is another article on migrations.

All about migration rollbacks.

How to add a column to an already existing table in Laravel using Migrations

1 thought on “Altering Table Through Migrations Laravel”

Leave a Comment

Your email address will not be published. Required fields are marked *

Tweet
Share
Share
Pin