How do I reverse a unique key added on multiple columns in Laravel? Basically, what should go in the down() function of this migration code:
public function up()
{
Schema::table('topics', function($table)
{
$table->unique(array('subject_id', 'grade_id', 'semester_id', 'name'));
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('topics', function($table)
{
}
}
To drop a unique index, you use dropUnique('name_of_index')
.
If you're not specifying an index name in the second parameter of unique()
, the name of the index will be tableName_fieldsSeperatedByUnderscore_unique
.
public function down()
{
Schema::table('topics', function($table)
{
$table->dropUnique('topics_subject_id_grade_id_semester_id_name_unique');
}
}
There are two approaches to drop unique index :
First Approach : In dropUnique() function we can pass array so that you don't need to use exact unique index name like "tableName_fieldsSeperatedByUnderscore_unique". Here is code snippet
Schema::table('users', function (Blueprint $table) {
$table->dropUnique(['email']);
});
This will drop the unique index of column 'email'.
Second Approach: This approach is exactly same as described by Marwelln,still I would like to put it here again. You can pass unique index name in the dropUnique(), it will also work. But be sure that you are confident about unique index name.Code for that looks like this:
Schema::table('users', function (Blueprint $table) {
$table->dropUnique('users_email_unique');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With