Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kyos's avatar
Level 6

Fakers - Random Digit Unique

I guys how can I use random unique digits with that code?

<?php

class ProfileTableSeeder extends Seeder {

public function run()
{
    $faker = Faker\Factory::create();

    foreach (range(1,50) as $index) {

        Profile::create([
            'user_id'           =>  $faker->randomDigit,
            'name'              =>  $faker->firstNameMale,
            'value_added_tax'   =>  $faker->randomDigit,
            'city'              =>  $faker->city,
            'zip_code'          =>  $faker->postcode,
            'country'           =>  $faker->country,
            'phone'             =>  $faker->phoneNumber,
            'img_src'           =>  $faker->imageUrl($width = 200, $height = 200)
        ]);

    }
}

}

0 likes
14 replies
bashy's avatar

Here's some random stuff for faker

https://github.com/fzaninotto/Faker#fakerproviderbase

Faker\Provider\Base

randomDigit // 7
randomDigitNotNull // 5
randomNumber($nbDigits = NULL) // 79907610
randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932
numberBetween($min = 1000, $max = 9000) // 8567
randomLetter // 'b'
randomElements($array = array ('a','b','c'), $count = 1) // array('c')
randomElement($array = array ('a','b','c')) // 'b'
numerify($string = '###') // '609'
lexify($string = '????') // 'wgts'
bothify($string = '## ??') // '42 jz'
10 likes
Shovels's avatar
Shovels
Best Answer
Level 6

I believe you can use something like:

$faker->unique()->randomDigit;
14 likes
bashy's avatar

Oh right they meant that...

Faker provides two special providers, unique() and optional(), to be called before any provider. optional() can be useful for seeding non-required fields, like a mobile telephone number; unique() is required to populate fields that cannot accept twice the same value, like primary identifiers.

1 like
kyos's avatar
Level 6

Yeah I use it.

public function run()
{
    $faker = Faker\Factory::create();

    foreach (range(1,50) as $index) {

        Profile::create([
            'user_id' => $faker->unique()->randomDigit,
            'name'  => $faker->firstNameMale,
            'value_added_tax'   => $faker->randomDigit,
            'city' => $faker->city,
            'post_code' => $faker->postcode,
            'country' => $faker->country,
            'phone' => $faker->phoneNumber,
            'img_src' => $faker->imageUrl($width = 200, $height = 200)
        ]);

    }
}

And it gives me an error: [OverflowException] Maximum retries of 10000 reached without finding a unique value

bashy's avatar

Never used unique() so not sure. Did you look at the examples for unique()?

kyos's avatar
Level 6

Yes they use this example:

// unique() forces providers to return unique values $values = array(); for ($i=0; $i < 10; $i++) { // get a random digit, but always a new one, to avoid duplicates $values []= $faker->unique()->randomDigit; } print_r($values); // [4, 1, 8, 5, 0, 2, 6, 9, 7, 3]

How can I use this on my current code.

Shovels's avatar

Try using:

$faker->unique()->randomNumber

You may find randomDigit only ever returns a single digit - As you've got 50 records it's gonna fail after the first 10.

2 likes
kyos's avatar
Level 6

Thats a nice ideia Shovels. Ive done this way and it works. In fact if I have only 50 users(fakers) I want 50 profiles right? So I do this:

    $faker = Faker\Factory::create();

    foreach (range(1,50) as $index) {

        Profile::create([
            'user_id' => $faker->unique()->numberBetween($min = 1, $max = 50),
            'name'  => $faker->firstNameMale,
            'value_added_tax'   => $faker->randomDigit,
            'city' => $faker->city,
            'post_code' => $faker->postcode,
            'country' => $faker->country,
            'phone' => $faker->phoneNumber,
            'img_src' => $faker->imageUrl($width = 200, $height = 200)
        ]);

    }

Change the value "numberBetween" if you want more than 50.

1 like
Shovels's avatar

Yeah that would work, but in this setup it would be more efficient to simply reference the index value for the user id. e.g.

foreach (range(1, 50) as $index)) 
{
    ...
    'user_id' => $index, // This will simply number your user_id's from 1 to 50
    ...
}

You'll need to flush the table each time you run the seeder though otherwise you're going to get duplicate IDs.

kyos's avatar
Level 6

yeah you´re right thats a cleaner way

Thomas's avatar

Anyone that's facing the same problem as above try this:

    $faker = Faker\Factory::create();
    $users = User::all();

    foreach (range(1,50) as $index) {

        Profile::create([
            'user_id' => $faker->unique()->randomElement($users->lists('id')),
            'name'  => $faker->firstNameMale,
            'value_added_tax'   => $faker->randomDigit,
            'city' => $faker->city,
            'post_code' => $faker->postcode,
            'country' => $faker->country,
            'phone' => $faker->phoneNumber,
            'img_src' => $faker->imageUrl($width = 200, $height = 200)
        ]);

         // Reset
         $faker->unique(true);
    }
5 likes
ElvisMagagula's avatar
$faker->randomElement(App\User::pluck('id', 'id')->toArray())
1 like
maanmuca's avatar

ElvisMagagula you are a Master ! , just a small change .... $faker->unique()->randomElement(User::pluck('id', 'id')->toArray())

1 like
nyayban's avatar

i want to seed phone number but number only (eg: 09498796541) how to write

Please or to participate in this conversation.