1

Is there any difference between

$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

and

$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

For me it's the same because foreign key is an index, am I right ?

Neabfi
  • 4,411
  • 3
  • 32
  • 42

2 Answers2

0

Reference to this question, it's good to index a foreign key, but database won't index it automatically.

So 2 code blocks above are different.

I think you can try to open your database schema and check for it.

Community
  • 1
  • 1
Đào Minh Hạt
  • 2,742
  • 16
  • 20
  • When I create a foreign key without any index, I see in my database that the foreign key is an index, that why I ask this question. I don't know exactly what is eloquent's behavior. – Neabfi Mar 06 '17 at 07:19
0

I found my answer:

Some databases by default put indexes on all created foreign keys automatically.

Eloquent only does a - create foreign key - command for and MySQL automatically adds the index.

Thanks to Mittensoff from laracasts for the answer ;)

Neabfi
  • 4,411
  • 3
  • 32
  • 42