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)
]);
}
}
}
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'
I believe you can use something like:
$faker->unique()->randomDigit;
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.
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
Never used unique() so not sure. Did you look at the examples for unique()?
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.
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.
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.
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.
yeah you´re right thats a cleaner way
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);
}
$faker->randomElement(App\User::pluck('id', 'id')->toArray())
ElvisMagagula you are a Master ! , just a small change ....
$faker->unique()->randomElement(User::pluck('id', 'id')->toArray())
i want to seed phone number but number only (eg: 09498796541)
how to write
Please sign in or create an account to participate in this conversation.