【发布时间】:2021-04-10 17:07:33
【问题描述】:
它在标签和可标记中插入表,我想要的只是插入到可标记(连接)表中。因为在它插入到 tagables 之前,有代码首先检查 tag 是否会插入到 tags 表中是否已经存在,如果存在就获取 id。为了让我的问题变得简单。我只是不包含检查标签是否存在的代码。
发布模型
public function tags(){ return $this->morphToMany( Tag::class, 'tagable', 'tagables', null, 'tag_id ); }
后控制器
// tags table theres a row id 1 with name greeting
$post = Post::create( ['body' => 'Hello World'] );
$post->tags()->create( ['tag_id' => 1] );
表格
// posts table
$table->mediumIncrements('post_id');
$table->string('body');
// tags table
$table->mediumIncrements('tag_id');
$table->string('tag_name');
//tagables table
$table->unsignedMediumInteger('tag_id');
$table->unsignedMediumInteger('tagable_id');
$table->string('tagable_type');
【问题讨论】:
标签: laravel