Add the column in the database table only if it's not present & can drop it if, its present

Now you can add the column in the database table only if its not present & can drop it if, its present. For that following methods are introduced:

👉 whenTableDoesntHaveColumn

👉 whenTableHasColumn

Available from Laravel 9.6.0

1return new class extends Migration {
2 public function up()
3 {
4 Schema::whenTableDoesntHaveColumn('users', 'name', function (Blueprint $table) {
5 $table->string('name', 30);
6 });
7 }
8 
9 public function down()
10 {
11 Schema::whenTableHasColumn('users', 'name', function (Blueprint $table) {
12 $table->dropColumn('name');
13 });
14 }
15}

Tip given by @iamharis010

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord