How can I add field using alter table on the migration laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Schema Changes in Laravel: How to Add Fields Using Migrations

As developers working with Laravel, managing database schema changes is a core responsibility. Whether you are building a new application or evolving an existing one, how you handle modifications to your tables—adding columns, modifying indexes, or dropping tables—directly impacts the stability and deployability of your code.

Many developers look for ways to bypass the standard migration process and use raw SQL commands like ALTER TABLE directly. While this seems appealing for immediate changes, as a senior developer, I strongly advocate for sticking to Laravel's built-in migration system. This approach ensures version control, reproducibility, and seamless deployment across all environments.

This post will walk you through the correct, idiomatic Laravel way to add a new field to an existing table, addressing your specific concern about deploying changes automatically.

The Laravel Philosophy: Why Migrations Rule Schema Changes

The core principle of using migrations is to treat your database schema as code. When you create a migration file, you are defining the desired state of the database. This provides several massive advantages over running arbitrary SQL commands:

  1. Version Control: Migrations live in Git. You track exactly how your database evolved, making rollbacks simple and history transparent.
  2. Reproducibility: Any developer can spin up a fresh environment and run php artisan migrate to achieve the exact same schema as the production server.
  3. Deployment Safety: When deploying code, running migrations ensures that the database structure is updated in a controlled, tested manner, preventing silent errors that raw SQL executions might cause.

While you can use DB::statement('ALTER TABLE ...') within a migration, it’s generally better to use the Schema Builder provided by Laravel, as it integrates seamlessly with the framework's context and abstraction layers. For more complex structural changes, understanding how tools like those found at laravelcompany.com structure their database interactions is key.

Adding a Field using Schema::table()

To add a new column to an existing table, you use the Schema::table() facade within your migration file. This method allows you to define changes relative to an existing table structure.

Let’s look at how you would modify your existing users table to add a mobile_number field:

<?php

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

class AddMobileNumberToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            // Add a new string column named 'mobile_number' with a length of 20 and allow null values
            $table->string('mobile_number', 20)->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            // When rolling back, drop the column we added
            $table->dropColumn('mobile_number');
        });
    }
}

Understanding the up() and down() Methods

  • up(): This method contains the instructions for making the change. In this case, we tell Laravel to look at the users table and add a new column named mobile_number.
  • down(): This is crucial for reversibility. If you ever need to roll back this migration (e.g., during testing or deployment rollback), the down() method tells Laravel exactly how to undo the change—in this case, dropping the newly created column.

Addressing Automatic Migration on Deployment

You mentioned wanting this change to be automatically applied upon merging code to development or master branches without manual intervention. This is precisely what the migration system facilitates!

When you commit a new migration file (like the one above) and push it to your repository, your deployment pipeline executes php artisan migrate. If this migration has not been run yet on the target server, Laravel will execute the up() method, safely adding the mobile_number column to the staging or live database.

If you were to use raw SQL (DB::statement(...)) directly without wrapping it in a formal migration structure, you would lose this automated, version-controlled safety net. Always rely on migrations for structural changes; they are the bridge between your application code and your database state.

Conclusion

To summarize, when adding fields to existing tables in Laravel, utilize the Schema::table() method within a dedicated migration. This practice ensures that all schema modifications are tracked, reversible, and integrated into your deployment workflow. By adhering to this pattern, you ensure that your database remains synchronized with your application code, making development, testing, and deployment significantly more robust. Keep building great applications by leveraging the power of Laravel's tools!