-1

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.

japose7523
  • 29
  • 2
  • 15
  • ``fake()->unique()->phoneNumber()`` ? – OMi Shah Nov 26 '22 at 07:38
  • https://stackoverflow.com/questions/44274460/faker-generating-strange-phone-numbers – Khayam Khan Nov 26 '22 at 07:46
  • Try this fake()>numerify('###-###-####'); https://fakerphp.github.io/formatters/numbers-and-strings/#numerify – birim Nov 26 '22 at 07:50
  • Does this answer your question? [Faker generating strange phone numbers?](https://stackoverflow.com/questions/44274460/faker-generating-strange-phone-numbers) – Khayam Khan Nov 26 '22 at 07:50
  • @OMiShah I used `'usr_mobile_phone' => fake()->unique()->phoneNumber(),` but returns **Data too long for column 'usr_mobile_phone' at row 1** – japose7523 Nov 26 '22 at 08:00
  • Since faker can generate longer than 11 characters for a phone number you can probably discard characters beyond the 11th when generating the number e.g. `substr(fake()->unique()->phoneNumber(), 0, 11);` – apokryfos Nov 26 '22 at 08:04
  • @apokryfos, trimming the length will give you strange outputs since numbers generated by faker contain dashes, brackets and plus sign. – OMi Shah Nov 26 '22 at 08:05
  • @japose7523, use ``fake()->unique()->e164PhoneNumber()`` to get 12 digit number with code and plus sign and then remove the plus sign. – OMi Shah Nov 26 '22 at 08:07

1 Answers1

1

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('##########')
OMi Shah
  • 5,768
  • 3
  • 25
  • 34