Laravel Phone
Laravel Phone stats
- Downloads
- 15.8M
- Stars
- 2,290
- Open Issues
- 0
- Forks
- 199
Adds phone number functionality to Laravel based on Google's libphonenumber API.
Laravel Phone Package Summary
Laravel Phone is a comprehensive Laravel package that enhances applications with sophisticated phone number handling capabilities. Built on the robust libphonenumber by Google, this package simplifies the integration of phone number validation, formatting, and utility functions into your Laravel projects.
Key Features
- Phone Number Validation: Easily validate phone numbers with options to limit valid numbers to specific countries, number types (like mobile or fixed_line), and lenient validation modes.
- Attribute Casting: Automatically cast phone number fields in Eloquent models to standardized formats or as raw input, facilitating easier management and storage.
- Utility Class: Utilize the
PhoneNumberutility class for comprehensive manipulation and comparison of phone numbers, including various formatting options and country-specific functionalities. - Database Storage and Retrieval: Store phone numbers in your preferred format (E.164, national, etc.) and retrieve or display them as needed, maintaining the integrity and usability of phone data.
Installation and Usage
To install, run:
composer require propaganistas/laravel-phone
Laravel will auto-discover the service provider. Add a validation line in your language files:
'phone' => 'The :attribute field must be a valid number.',
Validation Examples
You can specify validation rules directly in the model or form request:
'phonefield' => 'phone:US,BE',
For dynamic validation based on other input fields, configure as follows:
'phonefield' => 'phone','phonefield_country' => 'required_with:phonefield',
Validation can also be extended to include number types and leniency:
'phonefield' => 'phone:mobile,LENIENT',
Attribute Casting
Define how phone numbers should be cast in your Eloquent models:
class User extends Model{ public $casts = [ 'phone_1' => RawPhoneNumberCast::class.':BE', 'phone_2' => E164PhoneNumberCast::class.':BE', ];}
Utility Usage
The PhoneNumber class allows for extensive manipulation and formatting:
$phone = new PhoneNumber('012 34 56 78', 'BE');echo $phone->formatInternational(); // +32 12 34 56 78
Database Integration
Consider how phone numbers will be stored, retrieved, and searched in your database. The package supports various strategies for storing numbers in raw, national, or E.164 formats, and provides guidance on setting up searchable phone number fields.
Conclusion
The Laravel Phone package is a powerful tool for developers needing to handle phone number data within their Laravel applications, providing robust validation, formatting, and utility operations that integrate seamlessly with Laravel's Eloquent ORM and validation facilities.