Home » Add Column After A Column In Laravel Migration

Add Column After A Column In Laravel Migration

Last updated on May 31, 2021 by

In this article, we will see how to add column after column in the Laravel migration. At some level of project completion, you might need to add the new column into the database table then you can easily add that by creating new migration in Laravel.

But you want to add that column after the particular column for better understanding. So let’s see how to do it.

Laravel Migration Add Column After

Laravel provides the after() method which used to place a new column after another column.

Let’s create a new migration for users table and then we will add a new column into a newly created table for better understanding.

php artisan make:migration create_users_table

The above command will create a new migration in database/migrations directory. We have added the following code into the newly created migration.

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->text('title')->nullable();
            $table->text('first_name');
            $table->text('last_name');
            $table->string('email')->unique();
            $table->string('password')->nullable();
            $table->text('gender')->nullable();        
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Let’s now, we want to add the new 2 columns, middle_name after the first_name and the birth_date column after the password column. Let’s create a new migration to add these 2 columns.

php artisan make:migration update_users_table --table=users
<?php

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

class UpdateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->text('middle_name')->nullable()->after('first_name');
            $table->text('gender')->nullable()->after('password');
        });
    }

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

In the above example, we have added 2 new columns in the up() method which will be executed when we run the php artisan migrate command. While we are dropping both columns in the down() method which will be executed when we run the php artisan migrate:rollback command.

Add Multiple Columns After A Column In Migrations

Laravel 8.27 has introduced a new after method which allows you to add multiple new columns after an existing column at the same time:

$table->after('password', function ($table) {
    $table->string('address_line1');
    $table->string('address_line2');
    $table->string('city');
});

Previously, we have to reference each new column to get the order, for example:

Schema::table('customers', function ($table) {
    $table->string('address_line1')->after('password');
    $table->string('address_line2')->after('address_line1');
    $table->string('city')->after('address_line2');
});

You can see the crystal clear difference of code clarity.

Additionally, read our guide:

  1. Base Table Or View Already Exists In Laravel Migration
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How To Check Laravel PHP Version
  12. How To Handle Failed Jobs In Laravel
  13. How To Remove WooCommerce Data After Uninstall
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel
  16. How To Pass Laravel URL Parameter
  17. Laravel Run Specific Migration

That’s it from our end. We hope this article helped you to learn how to add column after a column in Laravel migration.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment