Home > database >  How to change column length of existing table in laravel?
How to change column length of existing table in laravel?

Time:10-01

I have one table which will contain some columns of some length, now I want to change the existing column length without dropping or hardcoded existing database table ,How to proceed please help me to acheive this thing

2020_02_13_065846_create_users_documents_table.php

public function up()
    {
        Schema::create('user_documents', function (Blueprint $table) {

            $table->increments('id');
            $table->string('upload_url', 200)->nullable();
            $table->string('user_name', 50)->nullable();
        });

Now I want to change column user_name length value to 200 by creating new table

CodePudding user response:

  1. Development solution

you correct it in that file and refresh your migration with php artisan migrate:fresh but you lose all the data in your database

  1. Production solution

Make a new migration with the alter statement.

public function up()
{
    \DB::statement('ALTER TABLE user_documents ALTER COLUMN upload_url VARCHAR (200)');
}

CodePudding user response:

you just have to modify you columns:

The change method allows you to modify the type and attributes of existing columns. For example, you may wish to increase the size of a string column

make a new Migration, set this statement:

Schema::table('users', function ($table) {
    $table->string('upload_url', 500)->change();
 $table->string('user_name', 500)->change();
});
  • Related