problem description
when the modelFactory of laravel generates false data, how to ensure that it will not be duplicated?
the environmental background of the problems and what methods you have tried
through unique Filter, but it didn"t work. Google checked the relevant problems and didn"t find a suitable one
.related codes
use Faker\Generator as Faker;
$factory->define(App\Models\Tab::class, function (Faker $faker) {
return [
"name" => $faker->unique()->randomElement(["", "", "", "", "", "", "", "", "", "", ""]),
"name_cover" => "http://imgbftv.b0.upaiyun.com/1342/20180515151342_1_45727.jpeg",
"name_cover_focus" => "http://imgbftv.b0.upaiyun.com/1342/20180515151342_1_45727.jpeg",
"bg_cover" => "http://imgbftv.b0.upaiyun.com/1342/20180515151342_1_45727.jpeg",
"operate_type" => $faker->randomElement([0, 1, 2]),
"status" => $faker->randomElement([0, 1, 2]),
"operator" => $faker->username,
"sort" => $faker->unique()->randomDigit,
];
});
public function run()
{
// create tabs and save them to the database
$tabs = factory(App\Models\Tab::class,8)->create();
}
what result do you expect? What is the error message actually seen?
some field data that is expected to be generated is not duplicated, but the current result is
with duplicate data.