Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename an index in Laravel 5

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

like image 851
Yahya Uddin Avatar asked Feb 06 '23 00:02

Yahya Uddin


1 Answers

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

like image 131
Yahya Uddin Avatar answered Feb 08 '23 14:02

Yahya Uddin