Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

Foreign id schema

When creating schemas and foreign keys, do you prefer the following:

$table->foreignId('category_id')->nullable()->constrained('categories')->onDelete('cascade');

Or do you simply go with:

$table->unsignedBigInteger('category_id')->index();

And then manually handle it through models?

0 likes
4 replies
Glukinho's avatar

I prefer #1, but simply go with #2 😆

Seriously, constrains keep your data consistent, you shouldn't neglect them.

JussiMannisto's avatar

Your 2nd example doesn't create a foreign key at all, so definitely not that.

martinbean's avatar

@developer654079525 Well the second example doesn’t actually create a foreign key; it’s just creating an unsigned integer column with zero constraints (whilst a foreign key is a type of index, an index itself is not a foreign key).

Please or to participate in this conversation.