I have a unique constraint in my database for two columns.
$table->unique(array("table_a_id", "table_b_id"));
Now I use Faker in a seeder class to fill in this pivot table:
$tableAIds = TableA::all()->lists("id")->toArray();
$tableBIds = TableB::all()->lists("id")->toArray();
foreach(range(1, 20) as $index) {
$tableAId = $faker->randomElement($tableAIds);
$tableBId = $faker->randomElement($tableBIds);
DB::table("table_a_table_b_pivot")->insert([
"table_a_id" => $tableAId,
"table_b_id" => $tableBId
]);
}
However this creates duplicates and the seeding fails due to SQL error.
How do I make sure I don't try to insert duplicates?