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
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
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