且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Laravel 5.5模型工厂播种数据透视表-mb_strtolower()期望参数1为字符串,给定数组

更新时间: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.