Laravel Migration Basic Indexes

Laravel Migrations Laravel Migration Basic Indexes

Basic Index

Indexes are used to find rows with specific column values quickly.

Laravel will automatically construct a basic index name based on the table, column names, and index type (basic) when creating an index. To drop an basic index, you must first give it a name.

Create Basic Index

Create a basic index with users_name_index name.

$table->index('name');

Drop Basic Index

users_name_index - is name of Basic Index.

  • users - table name
  • name - second column
  • index - type of index/key

We can drop basic index by index name.

$table->dropIndex('users_name_index');

Also we can drop basic index by column name. It will drop index users_name_index

$table->dropIndex(['name']); 

Create Composite Basic Index

Create a composite basic index with users_name_email_index name.

$table->index(['email', 'name']);

Drop Composite Basic Index

users_name_email_index - is name of Composite Basic Index.

  • users - table name
  • email - first column
  • name - second column
  • index - type of index/key
$table->dropIndex('users_name_email_index');

Creare Basic Index With Custom Name

MySQL has a limit here: 64 characters for basic index name length. You may need to set the index name yourself.

// basic index
$table->index('id', 'custom_basic_index_name');

// composite basic index
$table->index(['id', 'parent_id'], 'custom_basic_index_name');

Rename Index

$table->renameIndex('articles_id_parent_id_index', 'articles_id_parent_id_index_custom')