0

I want to add new Column with Migration after a custom column, for example I have this table structure:

Table Screenshot

And now I need to add a new column named badge after the prd_description (column 14).

So I ran php artisan make:migration add_badge_to_products_table --table=products

And here it goes:

public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->tinyInteger('badge')->unsigned()->nullable();
        });
    }

But now the problem is, I don't know how to add this column after that particular column. Because by default, Laravel adds this at the end of table.

So how to do this?

2 Answers2

2

You need to use after method as bellow

public function up()
{
    Schema::table('products', function (Blueprint $table) {
        $table->tinyInteger('badge')->unsigned()->nullable()->after('prd_description');
    });
}
parth
  • 1,803
  • 2
  • 19
  • 27
1

add column after a single specific column Like,

  Schema::table('products', function (Blueprint $table) {
        $table->tinyInteger('badge')->after('prd_description')->unsigned()->nullable();
    });

Also Add Mulitple column Like this, https://laravel.com/docs/8.x/migrations#column-order

Ankita Dobariya
  • 823
  • 5
  • 14