How To Drop Columns From Existing Table In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

Migrations are like version control for your database, allowing your team to easily modify and share the application's database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve. The Laravel Schema facade provides database agnostic support for creating ,deleting and manipulating tables across all of Laravel's supported database systems.

How To Drop Columns From Existing Table In Laravel Framework

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');
});

Drop single columns from a table by passing column name to the 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:

Example(2)
<?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.

Related Post