skip to Main Content

I have a table in my MySQL database with some existing columns. I would like to add a comment to one of these columns using Laravel migrations to provide additional information about its purpose.

I understand that I can add comments to new columns when creating a migration using the comment() method. However, I’m not sure how to add a comment to an existing column.

Can someone please provide an example of how to modify an existing column in a Laravel migration to add a comment to it? For example, I want to add a comment to the email column in my users table.

The database already exists, and I would like to apply the migration without recreating part of it, just updating it.

3

Answers


  1. first you require doctrine/dbal package install using below command

    composer require doctrine/dbal
    

    then create migration for making changes

    php artisan make:migration add_comment_to_email_column_in_users_table
    

    make changes in the created migration file

    Schema::table('users', function (Blueprint $table) {
         $table->string('email')->unique()->comment('email')->change();
    });
    

    then run

    php artisan migrate
    
    Login or Signup to reply.
  2. public function up() {
      Schema::table('product', function (Blueprint $table) { 
        $table->string('product_type')
            ->comment('0 = A Industrial Products, 1 = A Consumer Products')
            ->change();
      }); 
    }
    
    Login or Signup to reply.
  3. See document:
    https://laravel.com/docs/7.x/migrations#modifying-columns

    creating migration file

    <?php
    
    use IlluminateDatabaseMigrationsMigration;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateSupportFacadesSchema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::table('users', function (Blueprint $table) {
                $table->string('name', 255)->comment('comment added')->change();
            });
        }
    };
    

    then

    php artisan migrate
    

    comment would be added:

    mysql> show full columns from users;
    +-------------------+---------------------+--------------------+------+-----+---------+----------------+---------------------------------+---------------+
    | Field             | Type                | Collation          | Null | Key | Default | Extra          | Privileges                      | Comment       |
    +-------------------+---------------------+--------------------+------+-----+---------+----------------+---------------------------------+---------------+
    | id                | bigint(20) unsigned | NULL               | NO   | PRI | NULL    | auto_increment | select,insert,update,references |               |
    | name              | varchar(255)        | utf8mb4_unicode_ci | NO   |     | NULL    |                | select,insert,update,references | comment added |
    | email             | varchar(255)        | utf8mb4_unicode_ci | NO   | UNI | NULL    |                | select,insert,update,references |               |
    | email_verified_at | timestamp           | NULL               | YES  |     | NULL    |                | select,insert,update,references |               |
    | password          | varchar(255)        | utf8mb4_unicode_ci | NO   |     | NULL    |                | select,insert,update,references |               |
    | remember_token    | varchar(100)        | utf8mb4_unicode_ci | YES  |     | NULL    |                | select,insert,update,references |               |
    | created_at        | timestamp           | NULL               | YES  |     | NULL    |                | select,insert,update,references |               |
    | updated_at        | timestamp           | NULL               | YES  |     | NULL    |                | select,insert,update,references |               |
    +-------------------+---------------------+--------------------+------+-----+---------+----------------+---------------------------------+---------------+
    8 rows in set (0.02 sec)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search