I'm using Laravel 9 and I want to make a faker for my users
table:
public function definition()
{
return [
'usr_first_name' => fake()->name(),
'usr_last_name' => fake()->name(),
'usr_user_name' => fake()->unique()->name(),
'usr_mobile_phone' => , // generates a unique phone number
'usr_password_hash' => Hash::make('123456'),
'usr_email_address' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'usr_is_superuser' => now(),
'usr_is_active' => 1,
'usr_str' => Str::random(10),
'remember_token' => Str::random(10),
];
}
So as you can see for the column usr_mobile_phone
, I need to add a unique mobile number which has 11th character length.
But I don't know what is the command for doing that in faker!
So if you know, please let me know, thanks in advance.
You can use the fake()->e164PhoneNumber()
method to get a phone number (E164 format) with the plus sign and country code, based on the app locale which you can customise.
Now to get unique phone number we can combine the use of unique()
method: fake()->unique()
This should get you unique phone number:
fake()->unique()->e164PhoneNumber()
Example output: +14809888523, +12705838722, +13869134701
...now as per your requirement of 11 digit length, you can replace the plus sign:
'usr_mobile_phone' => str_replace('+', '', fake()->unique()->e164PhoneNumber())
Please note that different countries have different country code length, resulting in different phone number length output which you need to take care of yourself.
Alternatively, you can use the fake()->numerify()
method to generate dynamic format number by passing the the format to the method or by default format ###
:
fake()->unique()->numerfiy('##########');
Example output: 0733375159, 8270962398, 5125950018
...and then join the country code:
'usr_mobile_phone' => '1' . fake()->unique()->numerfiy('##########')