Comments (6)
@epalmans It all depends on your app's requirements. Is your application operation for one country or multiple? How fine-grained should searching phone numbers be? Do users expect to see their inputted value after saving, or are they fine with an after-save formatted value?
In the meantime I've taken the following approach that sort of keeps middle-ground in most scenarios. It requires some boilerplating though:
- 5 database columns:
phone
: holds the user input as-isphone_country
: holds the correlated countrynormalized_phone
: identical tophone
with everything but numerics strippednormalized_phone_national
: the formatted national phone number (usingformatNational()
and whitespaces stripped)normalized_phone_e164
: the E164 format of the phone number (usingformatE164()
)
All normalized columns can be set in a saving()
observer on the model.
Note: normalized_phone
can also be achieved by a virtual column and a REGEX function if your DB supports it (MySQL 8.0 for example).
Input forms interact with the phone
and phone_country
attributes. This way users are presented with their very own inputted phone number
Searches in phone numbers operate on normalized_phone
, normalized_phone_national
and normalized_phone_e164
alltogether using OR clauses. The search term is stripped first accordingly:
$query->where('normalized_phone', 'LIKE', preg_replace('/[^0-9]/', '', $term).'%')
->orWhere('normalized_phone_national', 'LIKE', preg_replace('/[^0-9]/', '', $term).'%')
->orWhere('normalized_phone_e164', 'LIKE', preg_replace('/[^+0-9]/', '', $term).'%');
To increase the hit rate you could potentially include some additional clauses using a parsed term (using phone()->formatXXXX()
) with a sensible country default.
Searching through a list of international phone numbers in a user friendly way is unfortunately quite cumbersome.
I'm not saying this is the best approach and there is surely room for optimization. It just covers a lot.
from laravel-phone.
E.164 is a format to globally and uniquely identify a phone number across the world. So yes, reverse formatting will always be possible to one of the provided formats. Itβs of course not possible to convert back to the way a user has once entered the value.
from laravel-phone.
You could use the following columns and workflow:
// Note the length of both columns; there's no need to accommodate for more.
// 36 is rather arbitrary; should be fine I think
// But there's definitely no need for the default 255.
$table->string('phone_number', 36)->nullable();
$table->string('phone_country_code', 2)->nullable();
$phone = phone($number);
- If a phone number is validated:
- Store the number in E.164 format:
$phone->formatE164()
- Store the country code:
$phone->getCountry()
- Store the extension, if present, separately
- If you expect to store phone numbers with extensions you might want to consider storing extensions separately
- Store the number in E.164 format:
- If a phone number is NOT validated:
- Store the number as-is
- Don't store a country code
- Searching: perform a two-fold search:
- Search for the string as-is (to capture non-validated phone numbers)
- Parse the string to E.164 format first and then search
- So something like this:
$database->where('phone_number', $string) ->orWhere(function($query) use ($string) { $parsed = phone($string, $country); // Supply country code here if applicable. $query->where('phone_number', $parsed->formatE164()) ->where('phone_country_code', $parsed->getCountry()); });
from laravel-phone.
@Propaganistas Can I pick your brain on this....? On a greenfield project, would you personally do it like this? To me, storing phonenumber in just a single column in E.164-format seems easier. Better even.... ? not sure
from laravel-phone.
Hi @Propaganistas . Many thanks for your elaborate answer!
And very good arguments indeed. Although, not fully applicable to my specific use-case: I don't care that much that users are seeing their own handcrafted formatting (sorry users π) and rather have the benefit of having to just manage one field.
That said, will your package always be able to correctly - and unambiguously - convert back from each E.164 formatted number? Or could one number have multiple possible outcomes (for example, due to overlapping country codes) ?
from laravel-phone.
thanks again @Propaganistas ! Great! π
from laravel-phone.
Related Issues (20)
- Your demo site seems broken HOT 1
- InvalidArgumentException : Missing country specification for phone attribute cast HOT 8
- How to only accept international format (E.164) for a specific country? HOT 2
- GH valid phone number starting with 025xxxxxxx fails validation, however 0256 works
- Validator fails on empty phone field, when required_if is present HOT 3
- Validation for Lebanese mobile numbers starting with 78 fails HOT 1
- When I used it, I found that the getCountryCode method was missing HOT 1
- Documented formatE164 method does not return expected result HOT 1
- Validation Message HOT 1
- how do i use AUTO with the class interface? HOT 1
- Failed to open stream: No such file or directory in /propaganistas/laravel-phone/src/Concerns/PhoneNumberFormat.php HOT 3
- "mobile" parameter no longer working in 5.1.0 when one of the data fields is "mobile" HOT 2
- Support for Laravel 11 HOT 1
- Laravel-Intl is unavailable HOT 2
- Simple phone validation fails
- Laravel tripping the + HOT 4
- Nullable rule doesn't work at request validation. HOT 1
- phone:INTERNATIONAL rule stopped working HOT 2
- Not a bug but feature request - expose the isPossibleNumberWithReason HOT 1
- Invalid number format passes validation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-phone.