Laravel Foreign relationships
Within Laravel there are many different ways to implement foreign keys. The way you choose to implement them doesn't matter, but it's important to be consistent.
So choose the one that works best for you and stick with it.
ForeignId and foreign
<?php
public function up(): void
{
Schema::create("books", function (Blueprint $table) {
$table->foreignId("author_id");
$table->foreign("author_id")
->references("id")
->on("authors");
});
}
php
UnsignedBigInteger and foreign
<?php
public function up(): void
{
Schema::create("books", function (Blueprint $table) {
$table->unsignedBigInteger("author_id");
$table->foreign("author_id")->references("id")->on("authors");
});
}
php
ForeignIdFor
<?php
public function up(): void
{
Schema::create("books", function (Blueprint $table) {
// The below is not correcty you need to reference an model
$table->foreignIdFor("authors");
});
}
php