- How do I install giggsey/libphonenumber-for-php-lite in Laravel?
- Install it with composer require giggsey/libphonenumber-for-php-lite. The package requires PHP 8.1 or newer and the mbstring extension. It is framework agnostic, so no Laravel service provider or config file is required; resolve PhoneNumberUtil::getInstance() where needed.
- Can I use libphonenumber-for-php-lite with Laravel validation?
- Yes. Create a custom Laravel validation rule that calls PhoneNumberUtil::getInstance()->parse() and then isValidNumber(). Catch NumberParseException and return false for unparseable input. For national-format input, pass the user's region code to parse(); for E.164 input beginning with +, a null region is typically fine.
- How should I store international phone numbers in a Laravel database?
- Normalize numbers to E.164 with PhoneNumberFormat::E164 before saving, for example +14155552671. Store the result in a string column and index it if you query by phone. You can do this in a model mutator, form request, or service layer so every write path uses the same normalization.
- How do I format phone numbers for display in Laravel?
- Parse the stored E.164 value and call format() with PhoneNumberFormat::NATIONAL or PhoneNumberFormat::INTERNATIONAL depending on context. Use E164 for APIs and storage, and a local or national format for user-facing pages when you know the country. Formatting does not replace validation, so validate before saving and formatting.
- What is the difference between libphonenumber-for-php-lite and the full package?
- The lite package includes the core phone number utilities for parsing, validating, and formatting. The full giggsey/libphonenumber-for-php package adds geolocation, carrier information, and short number info. Choose lite when you only need core number handling and want a smaller dependency.
- Which Laravel and PHP versions are supported?
- The lite package requires PHP 8.1 or newer and mbstring, and it is framework agnostic. It can be used in any Laravel version that runs on a supported PHP version, including modern Laravel 10, 11, and 12 applications. Composer will enforce the PHP requirement during installation.
- Can I validate phone numbers from multiple countries?
- Yes. For internationally formatted input beginning with +, parse() can infer the country. For local-format input, pass the correct ISO region code, such as US or GB, as the second argument to parse(). Then call isValidNumber() and optionally getNumberType() for extra context.
- How should I handle invalid phone numbers with this package?
- Catch NumberParseException for malformed input and treat isValidNumber() returning false as invalid. Decide whether your application should be strict and reject the value or lenient and preserve raw input separately. In most Laravel apps, return a validation error and avoid storing unvalidated numbers.
- Is libphonenumber-for-php-lite fast enough for production Laravel apps?
- For typical Laravel apps it is fast enough because it is pure PHP and avoids external services. The lite edition loads core phone metadata, so cache or reuse the PhoneNumberUtil instance rather than creating it repeatedly. If you process very high request volumes, benchmark parsing and formatting in your own environment.
- How do I test phone number validation in Laravel, and how often should I update the package?
- Write unit tests with known valid and invalid numbers for each region you support, and test your Laravel validation rule and model normalization paths. Because phone number rules come from Google's metadata, update the package with Composer periodically and rerun those tests after updates. If you see a disputed number, compare behavior with Google's libphonenumber demo and check package release notes.