PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

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

 April 13, 2022     laravel, laravel-8, laravel-migrations, migration, php     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing