Hello developers! In this guide, we'll see laravel 10 remove columns from the table using migration. Here, we'll learn about how to drop columns from the table using migration in laravel 10. Also, you can remove multiple columns using migration.
To drop a column, you may use the dropColumn
method on the schema builder.
1. Remove the 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('description');
});
}
/**
* 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(['title','description']);
});
}
/**
* 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', 'description')){
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('description');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
You might also like:
- Read Also: Laravel 10 Create Table Using Migration
- Read Also: Laravel 10 Add Column in Existing Table Example
- Read Also: How to Create CRUD with Image Upload in Laravel 10
- Read Also: Delete Multiple Records using Checkbox in Laravel 10