Laravel drop column if exists


Laravel drop column if exists – We can use migration to drop column if exists using the artisan command. Here in this tutorial we are going to explain how you can drop column in Laravel.


Laravel drop column if exists Using Migration | with full Example.

Let us understand how to drop drop column if exists Using Migration.

Full example of drop column using migration.

Now here i am going to explain how to drop column step by step.

Migration File:-

Let’s look at a simple example.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
			
        });
    }
     public function down()
    {
        if (Schema::hasColumn('users', 'name'))
        {
            Schema::table('users', function (Blueprint $table)
            {
                $table->dropColumn('name');
            });
        }
    }
}

Finally run the migration using command prompt.

php artisan migrate:rollback

Advertisements

Add Comment

📖 Read More