'Laravel factory error "Maximum retries of 10000 reached without finding a unique value" when I use unique Faker method

I have a Unit Seeder with this code in the run method: Unit::factory()->count(10)->create();

And the Unit Factory looks like this:

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()
        ];
    }
}

This thrown an OverflowException: "Maximum retries of 10000 reached without finding a unique value".

If there are 10 Units, and 10 items in the array... Why can't I get one unique id from the array for each Unit?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source