DEV Community

ilya
ilya

Posted on

Rename column in laravel migration

php artisan make:migration rename_fields_to_chat_messages_table --table=chat_message
You can created migration file with above command.
You can rename column as follow.

public function up()
{
    Schema::table('chat_message', function (Blueprint $table) {
        $table->renameColumn('created_date', 'created_at');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('chat_message', function (Blueprint $table) {
        $table->renameColumn('created_at', 'created_date');
    });
}
Enter fullscreen mode Exit fullscreen mode

you can update as follow

public function up()
{
Schema::table('chat_message', function (Blueprint $table) {
$table->unsignedInteger('user_id')->change();
$table->unsignedInteger('doc_id')->change();
$table->unsignedInteger('created_by')->nullable();
$table->unsignedInteger('updated_by')->nullable()->after('created_by');
$table->timestamp('created_at')->nullable()->change();
$table->timestamp('updated_at')->nullable()->after('created_at');
});
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('chat_message', function (Blueprint $table) {
        $table->dateTime('created_date')->change();
        $table->dropColumn('created_by');
        $table->dropColumn('updated_by');
        $table->dropColumn('updated_at');
    });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)