Laravel 7 表之间的物理关系不存在

Laravel 7 physical relationship between tables not present

我学习了关于多对多关系的教程 Laravel(在我的例子中是 7)。

效果不错,学到了很多东西,但奇怪的是我的表之间没有物理关系

我创建了一个多对多的关系,它应该 link 3 个表、产品、类别和 products_categories

我的问题:

1- 在数据库模式中有物理关系是必要的吗? 2- 如何让这些关系出现在我的图表中?

这是数据库模式的当前照片:

在这个数据库中,表之间有 links :

Laravel 关系与您的数据库关系(MySQL 或其他)不同。

您无需建立数据库关系即可让您的应用程序运行。这实际上取决于您要实现的目标。

如果要查看表之间的关系,请确保在迁移架构 (https://laravel.com/docs/7.x/migrations#foreign-key-constraints) 中指定外键,例如:

Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');

    $table->foreign('user_id')->references('id')->on('users');
});

对于数据透视表,您还可以使用如下迁移架构:

Schema::table('category_product', function (Blueprint $table) {
    $table->unsignedBigInteger('category_id');
    $table->unsignedBigInteger('product_id');
    $table->foreign('category_id')->references('id')->on('categories');
    $table->foreign('product_id')->references('id')->on('products');
});