ItSolutionStuff.com

How to Add Index in Laravel Migration?

By Hardik Savani β€’ April 16, 2024
Laravel

Here, i will show you how to works how to add mysql index in laravel migration. i would like to share with you laravel migration add index on column. you will learn laravel migration add unique index. let’s discuss about laravel migration create index. you will do the following things for laravel migration create unique index.

I will give you very simple example of how to create table with index column using laravel migration. you can easily use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

in this example, we will create items table and add index for title and created_at column. you can also add unique index using laravel migration. i will show you both example one by one.

Example 1: Simple Index

Create Migration Command:

php artisan make:migration create_items_table

database/migrations/2021_04_01_040458_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']);

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('items');

}

}

run migration

php artisan migrate

Output:

Example 2: Unique

Create Migration Command:

php artisan make:migration create_items2_table

database/migrations/2021_04_01_040458_create_items2_table.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateItems2Table extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->id();

$table->string('title');

$table->text('body');

$table->timestamps();

$table->unique(['title']);

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('items2');

}

}

run migration

php artisan migrate

Output:

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 8 Model Observers Tutorial Example

Read Now β†’
β˜…

How to use Model Events in Laravel 8?

Read Now β†’
β˜…

Laravel Migration - How to Add New Column in Existing Table ?

Read Now β†’
β˜…

How to Change Table Name using Laravel Migration?

Read Now β†’
β˜…

How to Remove Column from Table in Laravel Migration?

Read Now β†’
β˜…

How to Change Column Name and Data Type in Laravel Migration?

Read Now β†’
β˜…

How to Create Table using Migration in Laravel?

Read Now β†’
β˜…

Laravel Model Caching - Performance Boost Tutorial

Read Now β†’
β˜…

How to Call Model Function from Another Model in Codeigniter?

Read Now β†’
β˜…

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now β†’
β˜…

How to Add MySQL Trigger from Migration in Laravel?

Read Now β†’