Skip to content

All topics  /  Laravel

How to remove unique constraint from a column using Laravel migrations?

Laravel ·

Hi Guys,

In this example,I will learn you how to remove unique constraint from a column using laravel migrations.you can easy and simply remove unique constraint from a column using laravel migrations.

I have to remove a unique constraint from an email column using Laravel Migrations. Here is my code.I required doctrine/dbal in composer.json is it necessary to use it in migration files too or it will be called automatically.

Example 1:


class AlterEmailToUsers extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('email')->unique(false)->nullable()->change();
    });
}
/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {
       $table->string('email')->nullable(false)->unique()->change();
    });
}

Example 2:

$table->dropUnique('tableName_columnName_unique');
class AlterEmailToUsers extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropUnique('users_email_unique');
    });
}

It will help you...