我正在尝试为数据透视表创建 soem 播种机数据。下面的代码在某一点上会遇到重复错误。
有没有办法更好地做到这一点或改进此代码,以免造成重复。
$factory->define(Namespacehere\PostTag::class, function ($faker){
$postsId = $faker->randomElement(Namespacehere\Post::lists('id')->toArray());
$tagsId = $faker->randomElement(Namespacehere\Tag::lists('id')->toArray());
return [
'post_id' => $postsId,
'tag_id' => $tagsId
];
});
有错误
Integrity constraint violation: 1062 Duplicate entry
但在极少数情况下它会过去,但我想确保它一直都在。
这是在我的播种机类中运行的
public function run()
{
Namespacehere\PostTag::truncate();
factory(Namespacehere\PostTag::class, 30)->create();
}
谢谢