How to add new column to existing table in Laravel 8

How to add new column to existing table in Laravel 8 using migration commands

Posted by Ervin Adams on October 12, 2021

How to add new column to existing table in Laravel 8

How to add new column to existing table in Laravel 8 using migration commands

Posted by Ervin Adams on October 12, 2021

You will find that from time to time there is a need to add a new column to an existing table. In Laravel the migration script provides a convenient way to achieve this. In this lession you will learn how to create a new column in an existing table in Laravel 8.

Say for instance you are storing records of books in a book table. You have already created columns title and author but then there is a requirement to add the book's reference number. So, lets go ahead and add the a new column to the book table called reference_number.

Step 1. run artisan command

php artisan make:migration add_reference_number_to_books_table --table=books

Make sure to put the word add instead of create within the class name. Then go to  app/database/migrations folder and you see the result below.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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

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

Step 2 Amend the migration script by adding column to be created.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddReferenceNumberToBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->string('reference_number')->nullable();
        });
    }

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

Step 3 Run migration

php artisan migrate --path=database/migrations/2021_10_12_000000_add_reference_number_to_books_table.php

The file name generated by the migration was 2021_10_12_000000_add_reference_number_to_books_table.php. Therefore, be sure to put the name of your file in the path value.

If you now check the table you should see your new column.

If you wish to role back the new column you just created, execute command below:

php artisan migrate:rollback --step=1
This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question