How to write a migrate to undo the unique constraint in Laravel?

23,480

Solution 1

You have to do $table->dropUnique('users_email_unique');

To drop an index you must specify the index's name. Laravel assigns a reasonable name to the indexes by default. Simply concatenate the table name, the names of the column in the index, and the index type.

Solution 2

This the better way to drop unique. You can use the real column name here.

$table->dropUnique(['email']);
Share:
23,480
code-8
Author by

code-8

I'm B, I'm a cyb3r-full-stack-web-developer. I love anything that is related to web design/development/security, and I've been in the field for about ~9+ years. I do freelance on the side, if you need a web project done, message me. ;)

Updated on July 09, 2022

Comments

  • code-8
    code-8 almost 2 years

    I would do this to make my email field unique in the table.

    $table->unique('email');

    I've tried

    public function up()
    {
        Schema::table('contacts', function(Blueprint $table)
        {
            $table->dropUnique('email');
        });
    }
    

    Then, when I run php artisan migrate, I got this

    enter image description here

    It tell me that it's not there, but I'm 100% sure that it's there.

    enter image description here

    How do write a migration to undo that ?