How to Add a Column After a Specific Column in Laravel

As your application grows or when the requirements change, you may need to add a new column to an existing table. By default, when using an SQL database, a new column will be added at the end of the existing columns. However, you might prefer to have them in a specific order. This can be easily accomplished using a Laravel migration.

In this tutorial, we will walk you through the process of adding a new column, specifically a “publisher” column, after the “publish_date” column in a table called “books”. By following the steps outlined below, you’ll be able to effectively manage the order of your table columns.

Step 1: Create a Migration

To begin, open your terminal and navigate to your Laravel project directory. Then, execute the following Artisan command to generate a migration file:

php artisan make:migration add_publisher_column_to_books --table=books

The command used above generates a new migration file in the folder /database/migrations. Since we provided a --table=books parameter, it will conveniently autofill our table name in the migration file’s schema.

Screenshot of a Terminal Running artisan make:migration to Create a Migration

Step 2: Edit the Migration File

Locate the generated migration file in the database/migrations directory and open it. Inside the up method, utilize the Schema facade and Laravel’s Blueprint class to add the new column. To position the column after the “publish_date” column, use the after method. Here’s an example of the modified migration file:

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

class AddPublisherColumnToBooks extends Migration
{
    public function up()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->string('publisher')->after('publish_date');
        });
    }

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

Step 3: Run the Migration

Save the changes made to the migration file and return to your terminal. Execute the following command to run the migration and add the new “publisher” column after the “publish_date” column in the “books” table:

php artisan migrate

It should output the following:

Screenshot of a Terminal Running a Migration via Using artisan migrate

When we look at our table, before and after running the migration, we can see that it has achieved the desired result:

HeidiSQL View of Our Table Before Running the Migration
HeidiSQL View of Our Table After Running the Migration

Adding Multiple Columns After A Specific Column

Note that we can also write the migration to add several columns in our preferred position.

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

class AddPublisherColumnToBooks extends Migration
{
    public function up()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->string('publisher')->nullable()->after('publish_date');
            $table->string('edition')->nullable()->after('publisher');
            $table->string('language')->nullable()->after('edition');
        });
    }

    public function down()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->dropColumn('publisher');
            $table->dropColumn('edition');
            $table->dropColumn('language');
        });
    }
}

Note that Laravel 8.27 and newer allow for a shorter syntax, in which we only need one call to ->after() while specifying multiple columns. Therefore, we can use the following code to achieve the same result:

    public function up(): void
    {
        Schema::table('books', function (Blueprint $table) {
            $table->after('publish_date', function ($table) {
                $table->string('publisher')->nullable();
                $table->string('edition')->nullable();
                $table->string('language')->nullable();
            });
        });
    }

Conclusion

By following the steps outlined in this tutorial, you can easily add a new column after a specific column in your SQL table using Laravel migrations. In our example, we demonstrated how to add a “publisher” column after the “publish_date” column in the “books” table. We also looked at an example of adding multiple columns while maintaining our preferred order.

Happy coding!

Johan van den Broek

Johan is the creator of laracoding.com. As a child, he began tinkering with various programming languages, many of which have been long forgotten today. Currently, he works exclusively with PHP, and his passion for programming remains to this day.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts