<?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']);
How to Add Index in Laravel Migration?
By Hardik Savani April 2, 2021 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenHere, 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.