問(wèn)題描述
laravel的modelFactory 生成假數(shù)據(jù)時(shí),如何保證不重復(fù)?
問(wèn)題出現(xiàn)的環(huán)境背景及自己嘗試過(guò)哪些方法
通過(guò)unique過(guò)濾,但是沒起作用,谷歌了相關(guān)問(wèn)題,沒有發(fā)現(xiàn)合適的
相關(guān)代碼
use Faker\Generator as Faker;
$factory->define(App\Models\Tab::class, function (Faker $faker) {
return [
'name' => $faker->unique()->randomElement(['我的', '搜索', '推薦', '電影', '電視劇', '綜藝', '小品', '動(dòng)漫', '動(dòng)畫屋', '應(yīng)用', '其他']),
'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();
}
你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?
期待生成的某些字段數(shù)據(jù)是不重復(fù)的,但目前的結(jié)果是有重復(fù)數(shù)據(jù)的
laravel的modelFactory 生成假數(shù)據(jù)時(shí),如何保證不重復(fù)
翻翻過(guò)去那場(chǎng)雪
2019-03-09 13:24:19