Dropping Columns
To drop a column, use the dropColumn
method on the schema builder. Before dropping columns from a SQLite database, you will need to add the doctrine/dbal
dependency to your composer.json
file and run the composer update
command in your terminal to install the library:
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});
dropColumn
method:
Example(1)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestTable extends Migration
{
public function up()
{
Schema::table('test', function (Blueprint $table) {
$table->dropColumn(['desc']);
});
}
public function down()
{
//
}
}
Drop multiple columns from a table by passing an array of column names to the dropColumn
method:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestTable extends Migration
{
public function up()
{
Schema::table('test', function (Blueprint $table) {
$table->dropColumn(['desc','test_name','test_type']);
});
}
public function down()
{
//
}
}
Drop columns from table If its Exists
Example(3)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestTable extends Migration
{
public function up()
{
if (Schema::hasColumn('test', 'desc')){
Schema::table('test', function (Blueprint $table) {
$table->dropColumn(['desc']);
});
}
}
public function down()
{
//
}
}
Available Command Aliases
Command | Description |
---|---|
$table->dropMorphs('morphable'); |
Drop the morphable_id and morphable_type columns. |
$table->dropRememberToken(); |
Drop the remember_token column. |
$table->dropSoftDeletes(); |
Drop the deleted_at column. |
$table->dropSoftDeletesTz(); |
Alias of dropSoftDeletes() method. |
$table->dropTimestamps(); |
Drop the created_at and updated_at columns. |
$table->dropTimestampsTz(); |
Alias of dropTimestamps() method. |
Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported.