如何在单个 table 中两次使用相同的外键?

how can I use the same foreign key twice in a single table?

我遇到了一个问题。所以我在 Laravel:

中进行了迁移
    {
        Schema::create('transfers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('from_inventory_id')->constrained();
            $table->foreignId('to_inventory_id')->constrained();
        });
    }

我想做的是将产品从库存转移到另一个库存。问题是,如果我按照上面的方式命名它们,Laravel 将不会将它们识别为外键,因为它们必须是 'inventory_id',但我也不能将两个列命名为相同.

我怎样才能随意命名它们并为它们应用相同的外键?

您应该使用旧语法:

$table->unsignedBigInteger('from_inventory_id');
$table->foreign('from_inventory_id')->references('id')->on('inventories');
$table->unsignedBigInteger('to_inventory_id');
$table->foreign('to_inventory_id')->references('id')->on('inventories');