Add New Column By Checking the Column Already Exists or Not in Laravel

Hello artisan,

In this laravel migration tips tutorial, I will share with you a good way to create a new column in your existing table in laravel. Sometimes we need to create a new column in our table.

We all know that how to create a new column in laravel without losing data. But in this example i will show you that how to create a new column in our existing table with checking that the column already exists or not.

Let's see the example code:

public function up()
{
  if (Schema::hasTable('your_table')) {
    Schema::table('your_table', function (Blueprint $table) {
      if (!Schema::hasColumn('your_table', 'your_new_column')) {
	     $table->timestamp('your_new_column');
      } 
    });
  }
}

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js