How to Add a New Column to an Existing Table in Laravel

How to Add a New Column to an Existing Table in Laravel

Add a new column to an existing table in Laravel using migrations while the application is live. This step-by-step tutorial shows you how to add a views column to a posts table, including creating the migration, editing the schema, and running the migration with artisan commands. Perfect for beginners and experienced Laravel developers alike. Its pretty straight forward when you have not run the migrations and if not, then you can simply add one column into the migration file of your table. In cases, you have already migrated and want to add the column cleanly through laravel follow along.


Example 1: Add views Column to posts Table

1. Create a New Migration

Run this in your terminal:

php artisan make:migration add_views_to_posts_table --table=posts

2. Edit the Migration File

Laravel creates a new file in database/migrations/. Open it and update the up() and down() methods like this:

public function up()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->unsignedInteger('views')->default(0)->after('id');
    });
}

public function down()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->dropColumn('views');
    });
}
  • unsignedInteger: Because views shouldn’t be negative.
  • default(0): So existing rows are fine after the migration.
  • after('id'): Optional. Controls column order.

3. Run the Migration

php artisan migrate

That’s it. You’ve added a views column to the posts table.


Example 2: Add email column to users table

php artisan make:migration add_email_to_users_table --table=users

Open the newly created migration file and update the up and down functions like this:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('email')->unique()->after('name');
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('email');
    });
}

Available column modifiers

In this migration line:

$table->string('title')->nullable()->default('Untitled');
  • string('title') is the column type definition
  • nullable(), default() are column modifiers

Common Column Modifiers (as per Laravel docs):

ModifierDescription
->nullable()Allows storing NULL in the column
->default(value)Sets a default value for the column
->unsigned()Only allows positive integers
->unique()Adds a unique constraint
->index()Adds a regular index
->primary()Sets the column as primary key
->after('col')Puts the column after a specific column (MySQL only)
->first()Makes it the first column (MySQL only)
->comment('text')Adds a comment to the column
->change()Used when modifying an existing column
->nullable(false)Explicitly not nullable (Laravel 10+)