7

So I'm creating a database with Laravel and seeding it, and I ran into an issue where it generates a phone number as follows: (635) 889-5802 x45134. This causes issues by exceeding the desired length of the database column.

So my questions are:

  1. On a practical level, why does it do this? What is the real-world use case of x#####? I'm looking around and can't find a good explanation, best guess is an internal organization extension.
  2. When building an application where internationalization might eventually be important, is it a good idea to build in support for whatever these are? If so, then expanding the column width would be a better solution for me.
  3. If this isn't important, is there a way to prevent these x##### from being attached to the number on generation? I feel like this should be an option. If not it sounds like my best options would be a custom provider or a regex filter to remove these values.
dluxcru
  • 357
  • 1
  • 4
  • 16
  • Yup! That is the extension code for big buildings / offices. If your application will target businesses and organizations, more than likely these codes will be used and you will want to have support for those. You could also store the extension in a separate field/column. – Felix Guo May 31 '17 at 02:02
  • The extension is for an internal exchange, e.g., a phone system within a phone system. – Jared Farrish May 31 '17 at 02:16
  • In terms of "should you", I would support a field wide enough to accommodate the longest format. I would definitely [read this FAQ](https://github.com/googlei18n/libphonenumber/blob/master/FALSEHOODS.md) (from Michael Mior's answer) about how numbers can be highly variable, if you're wanting to parse and work with them (hence the library). Data storage is cheap. Actually, I think the reason why it's not optional is so you have to think about it. – Jared Farrish May 31 '17 at 02:20
  • Uff Dah! That's one of those things where it gets really annoying to deal with all of the use cases - the FAQ makes it clear this is more complicated than I might have liked. It's a real case of knowing where to draw the line for support. My application won't be making any calls on its own and majority of use cases will be US-only - my current thought is that I'll try and build proper support for all US numbers - as I'm open source users in other countries are welcome to submit pull requests to add support for quirks in their countries phone/zip etc. systems. – dluxcru May 31 '17 at 02:34

4 Answers4

3

x#### is commonly used to indicate a phone extension. From looking at the source code for the phone number provider, there's no option to remove the extension so you'd have to do so yourself.

However, you may want to consider reading Falsehoods Programmers Believe About Phone Numbers.

Michael Mior
  • 26,133
  • 8
  • 80
  • 110
  • Is it going to produce numbers in all those formats? – Jared Farrish May 31 '17 at 02:17
  • From what I've seen so far, that's not an outlandish bet. May submit an issue on the repository requesting further support for format selection. If you're not doing proper sanitization of phone numbers your data may have that kind of diversity, and I can see where having random formats in valuable for testing because users may submit phone numbers formatted differently if you don't format it for them. That said, some of us have narrower use cases than others - it makes sense to let us choose our format. – dluxcru May 31 '17 at 02:39
  • My guess would be the easiest approach would be to make your own provider with the format you want. It's not too challenging. Really you could just start with the existing provider and delete a lot of code. – Michael Mior May 31 '17 at 12:45
1

You need to ensure faker is using the correct locale. Try this:

use Faker\Factory as Faker;

//...

$faker = Faker::create('en_GB');

Faker will then generate GB compatible phone numbers etc

omarjebari
  • 3,097
  • 1
  • 24
  • 23
0

To avoid the problem your migration should be like this..

 $table->string('phone_number');

And your seeder should be like this.

'phone_number' => $faker->phoneNumber,
habib
  • 11
0

To answer your Question 3:

If you cannot or do not want to change the faker generation of phone numbers, you can write them to a file and filter out the unwanted extensions afterwards.

Based on @CharlesDuffy's answer to this question, the following will work in a bash shell:

$ while read -r; do echo "${REPLY%%x*}"; done <numbers >extensionsremoved

where 'numbers' is the name of your file containing lines ending with a fake phone number.

The assumption here is that 'x' always and only occurs in front of an extension at the end of a line. I tested it with 1000 fake numbers generated by the Python Faker, and it worked.

HendrikFrans
  • 133
  • 5