Laravel Migration Custom Index Name Example

By Hardik Savani April 16, 2024 Category : Laravel

Hello all!

In this article, we will talk about laravel migration change index name. we will help you to give example of laravel migration custom index name. you can see laravel custom index name migration. i explained simply about how to add custom index name in laravel migration. So, let's follow few step to create example of change index name laravel migration.

I will give you very simple example of how to change index name using migration in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

few days ago, i added one post, how to add index using laravel migration. in this post i will help you how to change index name using migration. let's see bellow example.

Create Migration Command:

php artisan make:migration create_items_table

database/migrations/2021_04_07_125911_create_items_table.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateItemsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->text('body');

$table->timestamps();

$table->index(['title', 'created_at'], 'items_title_created_at_index_new');

/*

$table->index(['title', 'created_at']);

Default Name Index Key Name will be : "items_title_created_at_index"

*/

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('items');

}

}

by default it will take "items_title_created_at_index" index key name, but if you want to change your own then index function take another argument for custom name so i just rename it like "items_title_created_at_index_new". let's run seeder and see mysql layout.

run migration

php artisan migrate

I hope it can help you...

Shares