ItSolutionStuff.com

How to Remove Column from Table in Laravel Migration?

By Hardik Savani • April 16, 2024
Laravel

Hi Artisan,

In this quick example, let's see laravel migration remove column. This post will give you simple example of how to drop column in laravel migration. i would like to show you remove column laravel migration. We will use drop field laravel migration.

you can easily drop column from database table in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

I will give you some example that way you can easily remove column using migration. let's see bellow example that will help you.

1) Remove Column using Migration

2) Remove Multiple Column using Migration

3) Remove Column If Exists using Migration

1) Remove Column using Migration

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class ChangePostsTableColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('posts', function (Blueprint $table) {

$table->dropColumn('body');

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

2) Remove Multiple Column using Migration

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class ChangePostsTableColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('posts', function (Blueprint $table) {

$table->dropColumn(['body', 'title']);

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

3) Remove Column If Exists using Migration

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class ChangePostsTableColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

if (Schema::hasColumn('posts', 'body')){

Schema::table('posts', function (Blueprint $table) {

$table->dropColumn('body');

});

}

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Create Table using Migration in Laravel?

Read Now →

Laravel Check If Foreach is Empty Example

Read Now →

Laravel Blade Check If Variable is Set or Not Example

Read Now →

How to Write PHP Code in Laravel Blade?

Read Now →

Laravel Bail Rule | Stop Validation On First Failure

Read Now →

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now →

How to Add MySQL Trigger from Migration in Laravel?

Read Now →