09-Jan-2024
.
Admin
Hello Dev,
This post is focused on How to Create Laravel 10 Migration with a Custom Index Name. I explained simply step by step How to change/disable autogenerated index names. if you have a question about Laravel Migration Add Index on Column Example then I will give a simple example with a solution. you'll learn Laravel Migration Tutorial: Custom Index Names Made Simple. Let's see the below example of How to Craft Migrations with Custom Index Names.
I will provide a straightforward example of how to change the index name using migration in Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10 versions.
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 use the index key name 'items_title_created_at_index.' However, if you wish to customize it, the index function accepts an additional argument for a custom name. In this example, I've renamed it to items_title_created_at_index_new. Let's run the seeder and observe the MySQL layout.
run migration
php artisan migrate
I hope it can help you...
#Laravel 10