我有一个 Unit Seeder,其 run 方法中包含以下代码:Unit::factory()->count(10)->create();
单元工厂看起来像这样:
class UnitFactory extends Factory
{
protected $model = Unit::class;
public function definition()
{
// $unitUbitactionIds = UnitUbication::pluck('id');
$testIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
return [
'unit_ubication_id' => $this->faker->unique()->randomElement($testIds),
'name' => $this->faker->word()
];
}
}
这引发了 OverflowException:“最大重试次数达到 10000 次,但未找到唯一值”。
如果有 10 个单元,数组中有 10 个项目...为什么我不能从数组中为每个单元获取一个唯一的 id?
您需要使用:
$this->faker->unique()->numberBetween(1, 10)
instead of ... ->randomElement($testIds)...
对于小数据集,随机元素()方法是不同的 from numberBetween()它会多次从列表中随机生成相同的值,并在循环中重复,这会导致错误,具体报告已达到搜索唯一值的限制。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)