Reputation: 28861
I want to rename an index in Laravel 5.
In a previous migration a column was created for table named a
like so:
$table->unsignedInteger('foo')->index('blah');
I want to rename the index so that it uses the default Laravel notation.
i.e. I want to rename the blah
index to a_blah
.
I know how to rename a normal column, like so:
$table->renameColumn('from', 'to');
But the documentation does not mention how to rename indexes.
How can I do this?
UPDATE
It seems that Laravel does not support this natively. Please upvote the issue:
https://github.com/laravel/internals/issues/443
Upvotes: 7
Views: 6781
Reputation: 28861
Laravel 5.6 now supports renaming indexes:
$table->renameIndex('from', 'to')
https://laravel.com/docs/5.6/migrations#renaming-indexes
Old Answer for Laravel <= 5.5
As @KuKec mentioned, it seems Laravel does not have native support for renaming indexes. But you can use raw SQL. For example in MySQL it would be like so:
DB::statement('RENAME INDEX old_index TO new_index');
This would also likely be more efficient than dropping and re-indexing (especially on large databases).
I have also created an issue for the feature request so this is better supported. Please upvote it here: https://github.com/laravel/internals/issues/443
Upvotes: 9
Reputation: 4610
I think Laravel doesn't support any method for renaming index, you should drop the old index blah
$table->dropIndex('blah');
and make new one with desired name a_blah
.
$table->index('foo', 'a_blah');
Upvotes: 4
Reputation: 61
You should first:
$table->dropIndex('your_full_name_index')
then do
$table->index('new_index_name')
Upvotes: 1