I ma trying to create soem seeder data for a pivot table. The code below works to one point that it will hit a duplication error.
Is there a way to do this better or to improve this code so not to cause duplication.
$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
];
});
There error is
Integrity constraint violation: 1062 Duplicate entry
But on the rare occasion it passes, but i want to make sure it does all the time.
This is run with in my seeder class
public function run()
{
Namespacehere\PostTag::truncate();
factory(Namespacehere\PostTag::class, 30)->create();
}
Thanks