mySun
mySun

Reputation: 1696

How to create unique indexes in Laravel?

I create table with migration in laravel 5.6.

I need create a unique indexes with migration.

My code is:

$table->index([
            'system',
            'code',
            'city',
            'seat',
            'type'
        ], 'PrimaryPayment');

But after run migrate . in phpmyadmin -> table -> table_name -> indexes show unique: No

How to create unique indexes?

Upvotes: 12

Views: 17291

Answers (2)

Bhargav Raviya
Bhargav Raviya

Reputation: 399

if your are using unique with index it not best pratice

don't use this

$table->string('slug')->unique()->index();

please using only unique beacuse include unique() already creates an index, the extra index() is unnecessary

like that

$table->string('slug')->unique();

and for multi fields

$table->unique(['system', 'code', 'city', 'seat', 'type'],'PrimaryPayment');

don't assign defaut value of 'PrimaryPayment' (not recommending)

Upvotes: 0

Sohel0415
Sohel0415

Reputation: 9853

You can use unique() method on migration:

$table->string('email')->unique();

Or like this for compound unique index:

$table->unique([
        'system',
        'code',
        'city',
        'seat',
        'type'
    ], 'PrimaryPayment');

Upvotes: 23

Related Questions