【发布时间】:2021-07-05 22:55:06
【问题描述】:
我遵循 laravel 文档中的教程,但似乎教程没有完整解释参数 morphToMany 以明确确定函数应指向的内容。
SQLSTATE[42S22]:未找到列:1054 未知列 '字段列表'中的'tagables.tagables_id'(SQL:选择
tags.*,tagables.tagables_id作为pivot_tagables_id,tagables.tag_tag_id作为pivot_tag_tag_id,tagables.tagables_type作为pivot_tagables_type来自tags内部 在tags.tag_id上加入tagables=tagables.tag_tag_idtagables.tagables_id= 1 和tagables.tagables_type= 应用\模型\漫画)
媒体表
Schema::create('media', function (Blueprint $table) {
$table->mediumIncrements('media_id)->nullable(false);
$table->string('title', 255);
});
标签表//标签内的一行(步行)
Schema::create('tags', function (Blueprint $table) {
$table->mediumIncrements('tag_id');
$table->string('tag_name', 255);
});
标签表
Schema::create('tagables', function (Blueprint $table) {
$table->unsignedMediumInteger('tag_id')->nullable(false);
$table->unsignedMediumInteger('tagable_id')->nullable(false);
$table->string('tagable_type', 255)->nullable(false);
});
漫画模型
public function tags()
{
return $this->morphToMany(Tag::class, 'tagable');
}
漫画控制器
// insert new comic
$comic = Comic::create([
'title' => 'Doraemon',
]);
// Insert into tagables table with current comic id and bind to tag id 1
which is walking
$comic->tags->create([
'tag_id' => 1
]);
插入媒体成功,插入tagables表失败。
【问题讨论】:
标签: laravel