- How do I install and set up Laravel-Phone in a Laravel 13 project?
- Run `composer require propaganistas/laravel-phone`, then add the validation rule translation to your language files (e.g., `validation.php`). The package auto-discovers the service provider, so no manual registration is needed. Test with a simple rule like `phone:US` in your validation logic.
- What Laravel versions does this package support?
- Laravel-Phone is fully compatible with Laravel 10+, including Laravel 13. It requires PHP 8.1+ due to dependencies like `libphonenumber-for-php-lite`. Always check the latest release notes for version-specific updates.
- Can I validate phone numbers by country or type (e.g., mobile, landline)?
- Yes. Use country codes like `phone:US,BE` for specific countries or add types like `phone:US,mobile`. For dynamic validation, pair phone fields with country fields (e.g., `user_phone` and `user_country`) using the `_country` suffix convention.
- How should I store phone numbers in the database—raw input or E.164 format?
- Store E.164 format for uniqueness and consistency (e.g., `+14155552671`). If you need raw input for user editing, store both `phone_raw` and `phone_country` columns, then derive E.164 via the `PhoneNumber` utility class. This balances validation and user experience.
- Does this package work with Eloquent model casting? How do I cast a phone field?
- Yes. Use `E164PhoneNumberCast` for normalized storage or `RawPhoneNumberCast` if you need raw input. Add the cast to your model’s `$casts` array, e.g., `protected $casts = ['phone' => E164PhoneNumberCast::class];`. The package handles conversion automatically.
- What’s the performance impact of phone validation in bulk operations?
- Validation is lightweight for most cases, but heavy operations (e.g., LENIENT mode) may slow bulk inserts. Benchmark your workflows. For high-throughput apps, consider caching validated numbers or deferring validation until necessary.
- How do I format phone numbers for display or dialing in Blade templates?
- Use the `PhoneNumber` utility class methods like `$user->phone->formatNational()` for display or `$user->phone->formatForMobileDialingInCountry('US')` for clickable links. Example: `<a href="tel:{{ $user->phone->formatE164() }}">{{ $user->phone->formatInternational() }}</a>`.
- Are there alternatives to this package? Why choose Laravel-Phone?
- Alternatives include direct use of `giggsey/libphonenumber-for-php`, but Laravel-Phone provides Laravel-specific integrations (validation rules, Eloquent casts, Blade helpers) and a cleaner API. It’s actively maintained and designed for Laravel’s ecosystem.
- How do I handle invalid phone numbers in validation errors?
- Invalid numbers trigger Laravel’s default validation errors. Customize error messages in your language files (e.g., `'phone' => 'The :attribute must be a valid number.'`). For logging, extend the `Phone` rule class or use Laravel’s `failed` event listeners.
- Can I dynamically detect a user’s country for phone validation?
- Yes. Use the `_country` suffix convention (e.g., `user_phone` and `user_country`) or manually pass a country field to the validator. Example: `'phone' => ['phone', 'country_field:user_country']`. This enables context-aware validation.