在laravel中迁移多个到多个多态关系的问题

问题描述 投票:-1回答:1

我有分类模型,我想将它用于帖子和主题。我认为应该使用多对多的多态关系,但在迁移中我得到这个错误:

  Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forum.categoriables' doesn't ex
ist (SQL: alter table `categoriables` add `category_id` int unsigned not null, add `categoriable_id` int unsigned not null, add `ca
tegoriable_type` varchar(191) not null)

这是类别表:

 Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();

            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->unsignedInteger('parent_id');
            $table->foreign('parent_id')->references('id')->on('topics')->onDelete('cascade');

            $table->timestamps();
        });

这是分类表:

Schema::table('categoriables', function (Blueprint $table) {
            $table->unsignedInteger('category_id');
            $table->unsignedInteger('categoriable_id');
            $table->string('categoriable_type');
        });

它们中的每一个都在单独的迁移文件中。

laravel many-to-many relation
1个回答
0
投票

我不太确定这是否是你想要的,但目前你正在“改变”可分类的。也许尝试改为Schema :: create?

Schema::create('categoriables', function (Blueprint $table) {
        $table->unsignedInteger('category_id');
        $table->unsignedInteger('categoriable_id');
        $table->string('categoriable_type');
    });
© www.soinside.com 2019 - 2024. All rights reserved.