- How do I install and set up Laravel Phone in my Laravel project?
- Run `composer require propaganistas/laravel-phone` to install. The package auto-discovers its service provider. Add a `'phone' => 'The :attribute field must be a valid number.'` translation entry to your `validation.php` language files for validation messages.
- What Laravel versions does this package support?
- The package supports Laravel 8.x, 9.x, and 10.x. Check the [Packagist page](https://packagist.org/packages/propaganistas/laravel-phone) for the latest version compatibility details.
- Can I validate phone numbers by country? How?
- Yes. Use `phone:US,BE` in your validation rules to restrict numbers to specific countries (e.g., US and Belgium). For dynamic country matching, use `phone:country_field` where `country_field` holds the country code, or pass a custom field name to the validator.
- What are the Eloquent cast types for phone numbers, and how do I use them?
- The package provides two casts: `RawPhoneNumberCast` (stores raw input) and `E164PhoneNumberCast` (stores E.164 format). Use them in your model like `$casts = ['phone' => E164PhoneNumberCast::class]` to automatically parse and validate phone numbers.
- How should I store phone numbers in the database for optimal search and validation?
- Store numbers in E.164 format (e.g., `+1234567890`) for consistency. For search flexibility, consider adding columns for national format (e.g., `020 1234 5678`) and raw input. The package doesn’t include migrations, so you’ll need to create these columns manually.
- Does this package handle SMS or voice API integrations like Twilio?
- No. Laravel Phone focuses solely on parsing, validation, and storage. For SMS/voice services, you’ll need to integrate third-party APIs like Twilio, AWS SNS, or Plivo separately.
- How do I format phone numbers for display (e.g., international vs. national format)?
- Use the `PhoneNumber` utility class. Call `PhoneNumber::format($number, 'INTERNATIONAL')` or `PhoneNumber::format($number, 'NATIONAL')` to get formatted strings. You can also specify a country code for regional formatting.
- What’s the best way to handle high-volume phone number validation (e.g., 10,000+ numbers/hour)?
- The underlying `libphonenumber` is CPU-intensive. For bulk validation, cache results using Redis or implement async processing (e.g., queues). Test performance under load before deploying to production.
- Can I compare two phone numbers for equality, even if formatted differently?
- Yes. Use `PhoneNumber::equals($number1, $number2)` to compare numbers regardless of formatting. This method normalizes inputs to E.164 format before comparison.
- Are there alternatives to this package for phone validation in Laravel?
- Alternatives include `egulias/email-validator` (for combined email/phone validation), `respect/validation` with custom phone rules, or standalone libraries like `monolog/monolog` (though not phone-specific). Laravel Phone stands out for its deep Google libphonenumber integration and Laravel-specific features like Eloquent casts.