laravel中迁移多对多态关系的问题

时间:2019-02-24 21:52:31

标签: laravel many-to-many relation

我有类别模型,我想将其用于帖子主题。 而且我认为应该使用多对多的多态关系,但是在迁移时会出现此错误:

  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');
        });

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

1 个答案:

答案 0 :(得分:0)

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

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