propaganistas/laravel-phone
Add robust phone number validation, casting, and formatting to Laravel using Google’s libphonenumber (PHP port). Validate by country or dynamic country fields, cast model attributes to phone objects, format numbers consistently, and compare/evaluate phone metadata.
phone:US,BE,mobile).required|string rules (e.g., phone:US).RawPhoneNumberCast/E164PhoneNumberCast).PhoneNumber class provides formatting/comparison without external dependencies.libphonenumber-for-php-lite is stable but not Laravel-specific.LENIENT mode) could impact bulk operations.phone:US,mobile) or lenient (LENIENT)?string rules with phone:US,BE in Form Requests/API resources.public function rules() {
return [
'contact_number' => 'required|phone:INTERNATIONAL,US,CA',
'user_phone' => ['phone:BE,mobile', 'required_with:user_country'],
'user_country' => 'required_with:user_phone|alpha:2',
];
}
phone_e164 column (uniqueness, but loses raw input).phone_raw, phone_country, and derived columns (e.g., phone_normalized).saving() to precompute searchable variants if needed.PhoneNumber methods (e.g., $phone->formatNational() in views).<a href="tel:{{ $user->phone->formatForMobileDialingInCountry('US') }}">
{{ $user->phone->formatNational() }}
</a>
phone:INTERNATIONAL for global support).E164PhoneNumberCast (or RawPhoneNumberCast if raw input is critical).phone_country column if needed).str_replace → $phone->formatE164()).PhoneNumber to DTOs/services for type safety.libphonenumber-for-php-lite.giggsey/libphonenumber-for-php (this package is a wrapper).| Priority | Task | Dependencies |
|---|---|---|
| High | Add package + validation rules | None |
| High | Update model casts | Database schema changes |
| Medium | Migrate existing phone data | Schema updates |
| Low | Add searchable variants | Observer setup |
| Low | Replace string formatting in views | Utility layer adoption |
libphonenumber fixes (e.g., new country codes).phone:US,BE,mobile) may need documentation.phpunit tests for phone validation (e.g., test/Feature/PhoneValidationTest.php).+1 (123) 456-7890 vs. 1234567890).E164PhoneNumberCast without country).phone:invalid_country).INTERNATIONAL mode).PhoneNumber objects).phone_e164 for uniqueness; avoid full-text search on phone fields.LENIENT mode for initial loads.PhoneNumber objects in services (e.g., PhoneNumberService).| Scenario | Impact | Mitigation |
|---|---|---|
| Invalid Country Code | Validation fails silently | Add fallback (e.g., phone:INTERNATIONAL) |
| Malformed Input | Database errors (e.g., E164PhoneNumberCast) |
Pre-validate or use try-catch |
| Schema Mismatch | Casts fail on load | Migrate data incrementally |
| Package Update Breaks | Enum changes in libphonenumber |
Test against dev-main branch |
| Search Performance | Slow queries on phone fields | Use derived columns (e.g., phone_normalized) |
formatNational(), formatForMobileDialingInCountry()).monolog channel).tel: links, international formats).RawPhoneNumberCast vs. E164PhoneNumberCast.How can I help you explore Laravel packages today?