Wednesday, April 13, 2022

[FIXED] How to add new column after a custom column existing at the DB

Issue

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?


Solution

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');
    });
}


Answered By - parth
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.