更新时间:2023-02-18 16:55:35
我认为您需要对数据透视表使用belongsToMany
I think you need to use belongsToMany for your pivot Table
在您的问题模型中
public function tags()
{
return $this->belongsToMany(Tag::class,'question_tag','tag_id','question_id');
}
与您的标记模型相同
public function questions()
{
return $this->belongsToMany(Question::class,'question_tag','question_id','tag_id');
}
另外,您需要更改
$question->tags()->sync(factory(App\Tag::class, 3)->make());
到
$tags = factory(App\Tag::class, 3)->make();
$tagIds = Tag::select('id')->get()->toArray();
$question->tags()->sync($tagIds);
这意味着您需要在Sync参数中传递ID.
which means you need to pass IDs in Sync argument.