Spread the love

Add or Update Index in Laravel Migration can be implement easily using the migrations libraries index method. Migrations provides almost all feature to create the database schema and create and update index in laravel migration. As We know database index is used to improve the speed of tables. Index can be create any column of table and it can also multiple in a table.

In our this article i will show you to add or update index on table columns in laravel migration. To create the index we need to add index in MySQL as follow

CREATE INDEX index_name ON table_name (column1, column2, ...)

And to create in laravel we need laravel migration schema class as follow

$table->index('user_id')->index('user_id');

//or for update on existing table

 $table->index('user_id');

Here, we used index method which accepts name of column so we added index to column user_id and for update the index on existing column we can direct call index method with column name.

In this post we will take a simple example in which we will create a tables and add the index to existing table or new table. This example will work all version of laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.

We will use the php artisan command to generate the Laravel migration and php artisan migrate command to modify the table.

Let’s understand it step by step

Step 1 : Generate migration file

To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command

php artisan make:migration movies_table

Above command will create a migration file in folder database/migrations

Output: 
Created Migration: 2022_06_30_174050_movies_table

Step 2 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to add index to column so adding a user_id column as integer and index.

so let’s open the file and start editing

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->id();
            $table->string('email')->unique();
            $table->integer('user_id')->index("user_id");
            $table->string('title');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::dropIfExists('movies');
    }
}

as you can see we have created integer field using $table->integer('user_id')->index("user_id"); syntax and created simple users table and added the index on it.

Update the index on existing table Laravel Migration

Update index on existing table column easy as adding to new column we just need to call index method directly and will pass the column name as follow

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->index("user_id");
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $table->dropIndex(['user_id']);
    }
}

Here we added index to already existing table and in down method we dropped the index using dropIndex method which accepts array of column name.

Step 3 : Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

This will create table in database and the output

Output: 
Migrating: 2022_02_15_174050_movies_table
Migrated:  2022_02_15_174050_movies_table (20.08ms)

Also Read : How to Create Relations in Laravel Migration?

Leave a Reply