ItSolutionStuff.com

How to Drop Index in Laravel Migration?

By Hardik Savani • November 5, 2023
Laravel

Hey Dev,

In this short tutorial, we will share the quick and straightforward way to how to drop index in laravel migration. step by step explain laravel migration drop index. I would like to share with you laravel migration drop index. Here you will learn laravel migration remove index. follow the below step for laravel migration remove index.

The Laravel migration provides the index() method to add a MySQL Index. Once you have added the index, you can also drop it. Here is a simple example of how to drop a index in Laravel migration using the dropIndex() method.

You will see the following examples:

1. Laravel Migration Add Index to Column

2. Laravel Migration Drop Index to Column

let's see the one by one.

Example 1: Laravel Migration Add Index to Column

Here, we will create products table with add Index to name column.

Create new migration using following command:

php artisan make:migration create_products_table

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

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

$table->id();

$table->string('name')->index();

$table->text('description');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('products');

}

};

Now, you are ready to run migration command:

php artisan migrate

You will see the layout as like the below:

Example 2: Laravel Migration Drop Index

Here, we will create products table with drop Index to code column.

Create new migration using following command:

php artisan make:migration change_datatype_column

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

Schema::table('products', function (Blueprint $table) {

$table->dropIndex('products_name_index');

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('products');

}

};

Now, you are ready to run migration command:

php artisan migrate

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Migration Unique Multiple Columns Example

Read Now →

How to Add Unique Constraint in Laravel Migration?

Read Now →

How to Add Boolean Column in Laravel Migration?

Read Now →

How to Change Column Position in Laravel Migration?

Read Now →

How to Migrate SQL File in Laravel Migration?

Read Now →

Laravel Migration Execute SQL Query Example

Read Now →

Laravel Migration Remove Default Value Example

Read Now →

Laravel Migration Change Datatype Timestamp to Datetime Example

Read Now →

Laravel Migration Change Datatype Date to Datetime Example

Read Now →

Laravel Migration Change Default Value Example

Read Now →

Laravel Migration Change Datatype Date to Timestamp Example

Read Now →

Laravel Migration Change Datatype String to Integer Example

Read Now →

Laravel Migration Custom Foreign Key Name Example

Read Now →