How To Drop Index Column In Existing Table With Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

The DROP INDEX statement is used to delete an index in a table.After a table has been created in a database, we find that it is advantageous to drop an index to that table to speed up queries involving this table . To do this in SQL, we specify that we want to change the table structure via the ALTER TABLE command, followed by the DROP INDEX command to tell the RDBMS that we want to drop an index .so in this article we are going to discuss how drop Index used in Laravel . Laravel schema builder supports several types of indexes.

How To Drop Index Column In Existing Table With Laravel Framework

Dropping Indexes

To drop an index, you must specify the index's name. By default, Laravel automatically assigns an index name based on the table name, the name of the indexed column, and the index type. Here are some examples:

 

Command Description
$table->dropPrimary('users_id_primary'); Drop a primary key from the "users" table.
$table->dropUnique('users_email_unique'); Drop a unique index from the "users" table.
$table->dropIndex('customer_country_index'); Drop a basic index from the "customer" table.



If you pass an array of columns into a method that drops indexes, the conventional index name will be generated based on the table name, columns and key type:

Schema::table('customer', function (Blueprint $table) {
       $table->dropIndex(['country']); // Drops index 'geo_state_index'
});


Example(1)

<?php

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

class CreateCustomerTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('customer', function (Blueprint $table) {
            $table->dropIndex(['country']); // Drops index 
        });
    }  
}

 

Related Post