How to add columns in existing table using migration in laravel

Created at 13-Apr-2021 , By samar

How to add columns in existing table using migration in laravel

Through many examples, we will learn how to resolve the "How to add columns in existing table using migration in laravel".

  • Add new columns in existing table using migration in laravel 8

    //1. Run php artisan make:migration command to create migration file.
    
    php artisan make:migration add_publish_to_comments --table=comments
    
    //2. Add columns to your migration file under - database\Migrations\<your_migration_filename>.php.
    
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) {
            $table->boolean('is_publish')->default(0);
        });
    }
    
    public function down()
    {
    	Schema::table('comments', function (Blueprint $table) {
    	    $table->dropColumn('is_publish');
    	});
    }
    
    //3. Migration command to create database table structure.</p>
    //Migrate to this file
    php artisan migrate --path=database\Migrations\2021_04_12_085751_add_publish_to_comments.php
    
    Or 
    //Migrate all files
    php artisan migrate
    

    You can add columns in existing table using the migration command php artisan make:migration add_publish_to_comments --table=comments. Use a specific migration file name which is not already exist in your migrations folder.  After that add columns to the migration file and run migrate command. This is how you can add columns to your existing database table in laravel without changing the table structure manually using PhpMyAdmin MySQL Database. Change table name, migration file name as per your requirements.

     

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.