In this article, we will learn the laravel remove columns from the table in Laravel migration. Laravel migrations are very useful to handle a database using simple commands.
Also, it is very handy in the development phase to seed fake data using Factories and Seeders. Moreover, you can easily move your database with one command in production. So let’s just jump into it.
Laravel: Remove Column From Table In Migration
To remove a column from the table in migrations, Laravel uses dropColumn method. This method accepts only 1 argument and that is single
column name or array of multiple
column names.
Notes: If you are using SQLite database then you must install the doctrine/dbal
package via the Composer package manager before the dropColumn
method may be used
Let’s take a simple example for a better understanding. Let’s say we have `users`
table and we have the following migration.
php artisan make:migration create_users_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();
$table->text('title')->nullable();
$table->text('first_name');
$table->text('middle_name')->nullable();
$table->text('last_name');
$table->string('email')->unique();
$table->string('password')->nullable();
$table->text('gender')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
After migrating the database we want to now remove the 2 columns `middle_name`
and `gender`
. Let’s generate the new migration to update the users
table by running the following command:
php artisan make:migration update_users_table
Example 01 Remove Column From Table Using Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('middle_name');
$table->dropColumn('gender');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->text('middle_name')->nullable()->after('first_name');
$table->text('gender')->nullable()->after('password');
});
}
}
The up()
method of migration will execute when we run the php artisan migrate
command and the down()
method will execute when we roll back the migration using php artisan migrate:rollback
.
Let’s now remove the multiple columns using only single dropColumn()
method.
Example 02 Remove Multiple Columns From Table Using Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['middle_name', 'gender']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->text('middle_name')->nullable()->after('first_name');
$table->text('gender')->nullable()->after('password');
});
}
}
Example 03 Remove Only If Columns Exists From Table Using Migration
Sometimes, while migration of database and rollback might generate weird errors. It might say the column doesn’t exist or already exists then you can go with this example.
Laravel provides the hasColumn('table_name', 'column_name')
function to check the column is exists in a given table or not. Let’ see an example.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
// The "users" table exists and has an "middle_name" column...
if (Schema::hasColumn('users', 'middle_name')) {
$table->dropColumn('middle_name');
}
if (Schema::hasColumn('users', 'gender')) {
$table->dropColumn('gender');
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
if (!Schema::hasColumn('users', 'middle_name')) {
$table->text('middle_name')->nullable()->after('first_name');
}
if (!Schema::hasColumn('users', 'gender')) {
$table->text('gender')->nullable()->after('password');
}
});
}
}
Additionally, read our guide:
- How to Select Data Between Two Dates in MySQL
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How To Check Laravel PHP Version
- How To Handle Failed Jobs In Laravel
- How To Remove WooCommerce Data After Uninstall
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
That’s it from our end. We hope this article helped you to remove a column from the table in Laravel migrations.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!