TALL Tales

How to Add a Column to an Existing Table

04 Oct, 2022

If you need to add in another column to your migration but don't want to rerun them all then you can create a migration that just adds in the extra field.

So, in your Terminal or other CLI run:

php artisan make:migration add_due_date_to_bills_table --table=bills

this will create a new migration for you.

Then in the migration you will see it has already created the up function:

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('bills', function (Blueprint $table) {
            
        });
    }

add in the field you are missing so that the up function becomes:

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('bills', function (Blueprint $table) {
            $table->date('due_date');
        });
    }

Add in the corresponding command for the drop function:

   /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('bills', function (Blueprint $table) {
            $table->dropColumn('due_date');
        });
    }

Then in your CLI you can run:

php artisan migrate

This will have added an extra column into your database table.

Photo by Abel Y Costa on Unsplash