0

如何使用自定义多态表创建多对多关系?

model_has_tags

$table->increments('id');
$table->integer('tag_id';
$table->string('model_type');
$table->integer('model_id');

标签

$table->increments('id');
$table->string('name');

用户

$table->increments('id');
$table->string('full_name');

我试试这个但不工作。

标签模型

public function users()
    {
        return $this->morphedByMany(User::class, 'model');
    }

用户模型

public function tags()
    {
        return $this->morphToMany(Tag::class, 'model');
    }

错误 :

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'stormport.models' doesn't exist (SQL: select `tags`.*, `models`.`model_id` as `pivot_model_id`, `models`.`tag_id` as `pivot_tag_id`....

如何解决这个问题?

4

1 回答 1

0

找到了解决方案:

用户模型

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'model', 'model_has_tags');
    }
于 2019-11-28T05:47:19.430 回答