---
description: >-
  https://www.itsolutionstuff.com/post/how-to-add-index-in-laravel-migrationexample.html
---

# How to Add Index in Laravel Migration? (ok)

> C:\xampp\htdocs\reset\database\migrations\2022\_05\_20\_184040\_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');
  }
}
```

Sau đây là ảnh sử dụng trước và sau

```
$table->index(['title', 'created_at']);
```

![](https://1592565776-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVfVRgKBlsVnWvhnDaa%2Fuploads%2FuBpN1NiEibnBbvjzL0nE%2FScreenshot_4.png?alt=media\&token=312fab14-36bf-4d7e-a88b-f8772610b2d9)

![](https://1592565776-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVfVRgKBlsVnWvhnDaa%2Fuploads%2Fm3hbfe0OoG7QYiZ0pqmJ%2FScreenshot_5.png?alt=media\&token=ebbef853-5402-433d-ad46-d47b43f06128)

## How to Add Index in Laravel Migration?

By Hardik Savani April 2, 2021 Category : Laravel![](https://a.vdo.ai/core/assets/img/cross.svg)PlayUnmuteLoaded: 1.15%Fullscreen[![VDO.AI](https://a.vdo.ai/core/assets/img/logo.svg)](https://vdo.ai/?utm_medium=video\&utm_term=itsolutionstuff.com\&utm_source=vdoai_logo)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 and laravel 9 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:**

![](https://www.itsolutionstuff.com/upload/laravel-migration-index.png)

**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

Read Also: [How to Add Foreign Key in Laravel Migration?](https://www.itsolutionstuff.com/post/how-to-add-foreign-key-in-laravel-migrationexample.html)

```
php artisan migrate
```

**Output:**

![](https://www.itsolutionstuff.com/upload/laravel-migration-index-new.png)

I hope it can help you...
