How To Add Index In Laravel 10 Migration

Websolutionstuff | May-03-2023 | Categories : Laravel MySQL

In this article, we will see how to add an index in laravel 10 migration. Here, we will learn about the laravel 10 add index using migration. The Laravel schema builder supports several types of indexes. let’s discuss laravel 10 migration to create an index.

So, let's see how to add an index in laravel 10 migration, laravel 10 migration add an index to an existing column, laravel 10 migration drop index, and how to add indexing in laravel 10.

Laravel provides an index() method for adding an index to column using migration.

Syntax:

$table->index('column_name');

Example:

$table->index('state');	

// Multiple columns
$table->index(['account_id', 'created_at']);

 

Example: Simple Index

In this example, we will create a simple index. So, create a migration and add an index to the column.

php artisan make:migration create_posts_table

Migration:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('description');
            $table->timestamps();
  
            $table->index(['title', 'created_at']);
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

 

Example: Unique Index

The following example creates a new email column and specifies that its values should be unique.

php artisan make:migration create_users_table

Alternatively, you may create the index after defining the column. To do so, you should call the unique method on the schema builder blueprint. This method accepts the name of the column that should receive a unique index

Migration:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
  
            $table->unique('email');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

When creating an index, Laravel will automatically generate an index name based on the table, column names, and the index type, but you may pass a second argument to the method to specify the index name yourself

$table->unique('email', 'unique_email');

 

Available Index Types

Laravel's schema builder blueprint class provides methods for creating each type of index supported by Laravel. Each index method accepts an optional second argument to specify the name of the index.

Command Description
$table->primary('id'); Adds a primary key.
$table->primary(['id', 'parent_id']); Adds composite keys.
$table->unique('email'); Adds a unique index.
$table->index('state'); Adds an index.
$table->fullText('body'); Adds a full text index (MySQL/PostgreSQL).
$table->fullText('body')->language('english'); Adds a full text index of the specified language (PostgreSQL).
$table->spatialIndex('location'); Adds a spatial index (except SQLite).

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Yajra Datatable Example Tutorial
Laravel 8 Yajra Datatable Exam...

In this article, we will see the laravel 8 yajra datatable example tutorial. Datatable provides users to many...

Read More

Sep-30-2020

How To Remove Spaces Using JQuery
How To Remove Spaces Using JQu...

In this article, we will explain to you how to remove extra space using jquery. many times we have requirements to...

Read More

Aug-10-2020

How To Send Email Using Markdown Mailable Laravel 9
How To Send Email Using Markdo...

In this article, we will see how to send email using markdown mailable laravel 9. we will learn laravel 9 to s...

Read More

Aug-05-2022

How To Bind Data In React JS
How To Bind Data In React JS

In this article, we will see how to bind data in React JS. Also, we will see how to bind the variable value in the...

Read More

Aug-19-2022