Laravel中每个人都有一个适合所有人或个人的Pivot?

时间:2018-03-06 13:19:21

标签: laravel relational-database

我不知道我在做对还是错...... 我想创建一个博客,其中包含多个类别和标签,具有多对多关系。

我应该为每个人制作一个数据透视表,如:

       "category_post"
        $table->integer('post_id')->unsigned()->index();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        $table->integer('category_id')->unsigned()->index();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

另一个用于Tags表... 或者我可以用一个可以过滤的额外列来制作一个表吗?

       "pivot_post"
        $table->integer('post_id')->unsigned()->index();
        $table->integer('pivot_id')->unsigned()->index();
        $table->string('type');

但..如果我可以为所有人制作一个,我不知道热门制作db和关系? 哪一个更适合表现..? 1大关系tableor多关系表? TNX。

1 个答案:

答案 0 :(得分:1)

如果您想通过单个表格完成此操作,我认为您正在寻找多态关系。 https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations

相关问题