### Install Laravel Phone Package via Composer Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This command installs the `propaganistas/laravel-phone` package via Composer, making its functionalities available in your Laravel application. The Service Provider is automatically discovered by Laravel. ```bash composer require propaganistas/laravel-phone ``` -------------------------------- ### Migrating from `PhoneNumber::make()` to `phone()` helper or constructor Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Provides examples for replacing the removed `PhoneNumber::make()` method with the `phone()` helper function or direct `PhoneNumber` object instantiation. ```PHP PhoneNumber::make($number, $country) // becomes phone($number, $country) // 1-to-1 replacement ; chainable with subsequent methods // or new PhoneNumber($number, $country) // wrap in additional parentheses to chain with subsequent methods ``` -------------------------------- ### Registering libphonenumber in Laravel Service Container Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Example of how to register `libphonenumber` as a singleton in Laravel's service container after its internal removal from the package. ```PHP $this->app->singleton('libphonenumber', function ($app) { return PhoneNumberUtil::getInstance(); }); ``` -------------------------------- ### Retrieving Information from PhoneNumber Objects Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Demonstrates methods to get information about a phone number, such as its type (e.g., fixed line, mobile) and its associated country. ```php $phone = new PhoneNumber('012 34 56 78', 'BE'); $phone->getType(); // libphonenumber\PhoneNumberType::FIXED_LINE $phone->isOfType('fixed_line'); // true (or use $phone->isOfType(libphonenumber\PhoneNumberType::FIXED_LINE) ) $phone->getCountry(); // 'BE' $phone->isOfCountry('BE'); // true ``` -------------------------------- ### Validate Phone Number with International and Specific Country Support Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP example demonstrates how to accept both internationally formatted phone numbers and numbers from specific whitelisted countries (e.g., Belgium). The `INTERNATIONAL` parameter allows for global numbers alongside country-specific validation. ```php 'my_input' => 'phone:INTERNATIONAL,BE', // 'my_input' => (new Phone)->international()->country('BE') ``` -------------------------------- ### Blacklist Specific Phone Number Types in Validation Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP example shows how to exclude certain phone number types from validation by prepending an exclamation mark to the type. This ensures that numbers matching the blacklisted type (e.g., `mobile`) will fail validation. ```php 'my_input' => 'phone:!mobile', // 'my_input' => (new Phone)->notType('mobile') // 'my_input' => (new Phone)->notType(libphonenumber\PhoneNumberType::MOBILE) ``` -------------------------------- ### Validate Phone Number with Specific Country Constraints Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP example demonstrates how to validate a phone number field (`my_input`) to ensure it belongs to specific countries (e.g., US, BE). It uses the `phone` rule with a comma-separated list of ISO 3166-1 alpha-2 country codes or the expressive `Phone` rule class. ```php 'my_input' => 'phone:US,BE', // 'my_input' => (new Phone)->country(['US', 'BE']) ``` -------------------------------- ### Instantiating PhoneNumber Objects Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Demonstrates how to create `PhoneNumber` objects directly using the constructor with a number and optional country, or conveniently via the `phone()` helper function. ```php use Propaganistas\LaravelPhone\PhoneNumber; (string) new PhoneNumber('+3212/34.56.78'); // +3212345678 (string) new PhoneNumber('012 34 56 78', 'BE'); // +3212345678 phone('+3212/34.56.78'); // PhoneNumber instance phone('012 34 56 78', 'BE'); // PhoneNumber instance phone('012 34 56 78', 'BE', $format); // string ``` -------------------------------- ### Migrating from `PhoneNumber::ofCountry()` to Constructor Parameter Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Shows how to adapt code that previously used the `ofCountry()` method, by passing the country directly to the `PhoneNumber` constructor or `phone()` helper. ```PHP $object = new PhoneNumber($number); $object->ofCountry($country); // becomes $object = new PhoneNumber($number, $country); // or phone($number, $country) ``` -------------------------------- ### PhoneNumberFormat Signature Changes Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Documentation for the updated `format($format)` method signature, detailing changes in parameter types, return types, and potential exceptions. ```APIDOC Method: format($format) Parameter $format: Before: string | int After: string | libphonenumber\PhoneNumberFormat Returns: string Throws: InvalidArgumentException: when $format is a string and could not be converted ``` -------------------------------- ### Updating Laravel Phone Rule `detect()` to `international()` Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Shows the change from the `detect()` method to `international()` for the `Rule::phone()` macro, reflecting a more descriptive naming convention. ```PHP 'phonefield' => Rule::phone()->detect() // becomes 'phonefield' => Rule::phone()->international() ``` -------------------------------- ### PhoneNumber::__construct() Signature Change (APIDOC) Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md The constructor signature for the `PhoneNumber` class has been updated. The `$number` parameter no longer accepts `null` and is now strictly `string`. The `$country` parameter's type has also been refined. ```APIDOC PhoneNumber::__construct(number: string, country: array|string|null) Before: Parameter $number: ?string Parameter $country: mixed Returns: PhoneNumber After: Parameter $number: string Parameter $country: array|string|null Returns: PhoneNumber ``` -------------------------------- ### PhoneNumber Class API Reference Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Detailed API reference for the `Propaganistas\LaravelPhone\PhoneNumber` class, including its constructor, various formatting methods, and information retrieval methods. Also includes the `phone()` helper function. ```APIDOC Propaganistas\LaravelPhone\PhoneNumber: __construct(string $number, string $country = null): PhoneNumber $number: The phone number string. $country: Optional. The country code (e.g., 'BE') if the number is not in international format. format(int $format): string $format: A constant from libphonenumber\PhoneNumberFormat (e.g., E164, INTERNATIONAL, NATIONAL). formatE164(): string Returns the phone number in E.164 format (e.g., '+3212345678'). formatInternational(): string Returns the phone number in international format (e.g., '+32 12 34 56 78'). formatRFC3966(): string Returns the phone number in RFC3966 format (e.g., 'tel:+32-12-34-56-78'). formatNational(): string Returns the phone number in national format (e.g., '012 34 56 78'). formatForCountry(string $countryCode): string $countryCode: The country code for which to format the number. Returns the number formatted for calling from the specified country. formatForMobileDialingInCountry(string $countryCode): string $countryCode: The country code for which to format the number for mobile dialing. Returns the number formatted for mobile dialing from the specified country. getType(): int Returns the phone number type (e.g., libphonenumber\PhoneNumberType::FIXED_LINE). isOfType(string|int $type): bool $type: The type to check against (e.g., 'fixed_line' or libphonenumber\PhoneNumberType::FIXED_LINE). Returns true if the number is of the specified type. getCountry(): string Returns the country code of the phone number (e.g., 'BE'). isOfCountry(string $countryCode): bool $countryCode: The country code to check against. Returns true if the number belongs to the specified country. Helper Function: phone(string $number, string $country = null, string $format = null): PhoneNumber|string $number: The phone number string. $country: Optional. The country code. $format: Optional. If provided, returns the formatted string; otherwise, returns a PhoneNumber instance. ``` -------------------------------- ### Using Laravel Phone Rule as an Object Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Demonstrates how to use the `Propaganistas\LaravelPhone\Rules\Phone` rule as a rule object in Laravel validation, including chaining methods like `mobile()` and `country()`. ```PHP 'phonefield' => (new Phone)->mobile()->country('BE') ``` -------------------------------- ### Formatting PhoneNumber Objects Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Illustrates various formatting methods available on the `PhoneNumber` object, including E.164, international, RFC3966, national, and country-specific formats for dialing. ```php $phone = new PhoneNumber('012/34.56.78', 'BE'); $phone->format($format); // See libphonenumber\PhoneNumberFormat $phone->formatE164(); // +3212345678 $phone->formatInternational(); // +32 12 34 56 78 $phone->formatRFC3966(); // tel:+32-12-34-56-78 $phone->formatNational(); // 012 34 56 78 // Formats so the number can be called straight from the provided country. $phone->formatForCountry('BE'); // 012 34 56 78 $phone->formatForCountry('NL'); // 00 32 12 34 56 78 $phone->formatForCountry('US'); // 011 32 12 34 56 78 // Formats so the number can be clicked on and called straight from the provided country using a cellphone. $phone->formatForMobileDialingInCountry('BE'); // 012345678 $phone->formatForMobileDialingInCountry('NL'); // +3212345678 $phone->formatForMobileDialingInCountry('US'); // +3212345678 ``` -------------------------------- ### Correct Order for Setting Phone Number and Country Attributes Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Highlights the importance of setting the country attribute *before* the phone number attribute when using `E164PhoneNumberCast` to avoid exceptions. This is due to Laravel's instant casting mechanism. ```php // Wrong $model->fill([ 'phone' => '012 34 56 78', 'phone_country' => 'BE', ]); // Correct $model->fill([ 'phone_country' => 'BE', 'phone' => '012 34 56 78', ]); // Wrong $model->phone = '012 34 56 78'; $model->phone_country = 'BE'; // Correct $model->phone_country = 'BE'; $model->phone = '012 34 56 78'; ``` -------------------------------- ### PhoneNumber::getType() Method Return Type Change (APIDOC) Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md The `getType()` method of the `PhoneNumber` class now returns a `libphonenumber\PhoneNumberType` enum instance instead of a simple string. This change reflects the underlying library's shift to enums. ```APIDOC PhoneNumber::getType() -> libphonenumber\PhoneNumberType Before: Returns: string After: Returns: libphonenumber\PhoneNumberType ``` -------------------------------- ### PhoneNumber::isOfType() Method Signature Change (APIDOC) Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md The `isOfType()` method's parameter `$type` now accepts `libphonenumber\PhoneNumberType` enum in addition to `string` or `int`. If an integer value was previously used, it must now be cast or converted to the enum. The method can now throw an `InvalidArgumentException` if a string type cannot be converted. ```APIDOC PhoneNumber::isOfType(type: string|libphonenumber\PhoneNumberType) -> bool Before: Parameter $type: string|int Returns: bool After: Parameter $type: string|libphonenumber\PhoneNumberType Returns: bool Throws: InvalidArgumentException (when $type is a string and could not be converted) ``` -------------------------------- ### Updating Laravel Phone Rule String Parameter `AUTO` to `INTERNATIONAL` Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md Illustrates the renaming of the `AUTO` parameter to `INTERNATIONAL` when defining the phone validation rule as a string. ```PHP 'phonefield' => 'phone:AUTO' // becomes 'phonefield' => 'phone:INTERNATIONAL' ``` -------------------------------- ### PHP: Compare PhoneNumber Objects for Equality Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This snippet demonstrates how to check if a `PhoneNumber` object is equal to another phone number using the `equals()` and `notEquals()` methods. It covers comparisons with strings (various formats) and other `PhoneNumber` objects, returning boolean results. ```php $phone = new PhoneNumber('012 34 56 78', 'BE'); $phone->equals('012/34.56.76', 'BE') // true $phone->equals('+32 12 34 56 78') // true $phone->equals( $anotherPhoneObject ) // true/false $phone->notEquals('045 67 89 10', 'BE') // true $phone->notEquals('+32 45 67 89 10') // true $phone->notEquals( $anotherPhoneObject ) // true/false ``` -------------------------------- ### Rename Fixed Line Phone Validation Shortcut Method (PHP) Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md The shortcut method for validating fixed line phone numbers has been renamed to align with snake_case conventions, matching the corresponding enum case more appropriately. Update calls from `fixedLine()` to `fixed_line()`. ```php (new Phone)->fixedLine(); // becomes (new Phone)->fixed_line(); ``` -------------------------------- ### Eloquent Model Attribute Casting for Phone Numbers Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Demonstrates how to configure Eloquent model attributes to automatically cast phone numbers using `RawPhoneNumberCast` and `E164PhoneNumberCast` classes, specifying the default country code for parsing. ```php use Illuminate\Database\Eloquent\Model; use Propaganistas\LaravelPhone\Casts\RawPhoneNumberCast; use Propaganistas\LaravelPhone\Casts\E164PhoneNumberCast; class User extends Model { public $casts = [ 'phone_1' => RawPhoneNumberCast::class.':BE', 'phone_2' => E164PhoneNumberCast::class.':BE', ]; } ``` -------------------------------- ### PHP: Laravel Observer for Phone Number Formatting Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This Laravel `saving()` observer method automatically formats and stores different variants of a phone number (normalized, national, E.164) before persistence. It ensures that `phone_normalized`, `phone_national`, and `phone_e164` columns are populated for searchability. ```php public function saving(User $user) { if ($user->isDirty('phone') && $user->phone) { $user->phone_normalized = preg_replace('/[^0-9]/', '', $user->phone); $user->phone_national = preg_replace('/[^0-9]/', '', phone($user->phone, $user->phone_country)->formatNational()); $user->phone_e164 = phone($user->phone, $user->phone_country)->formatE164(); } } ``` -------------------------------- ### Update String-based Phone Validation Rules (PHP) Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md When using string-based validation rules, references to `libphonenumber` constants must be updated to access the enum's value property or use the string literal. This change reflects the shift from class constants to native enums in `libphonenumber`. ```php 'phonefield' => 'phone:'.libphonenumber\PhoneNumberType::MOBILE, // becomes 'phonefield' => 'phone:'.libphonenumber\PhoneNumberType::MOBILE->value, 'phonefield' => 'phone:mobile', // or use the rule object ``` -------------------------------- ### Update Object-based Phone Validation Rules (PHP) Source: https://github.com/propaganistas/laravel-phone/blob/master/UPGRADING.md For object-based validation rules where phone types were referenced by their integer values, the code must now explicitly cast or convert the integer to the corresponding `libphonenumber\PhoneNumberType` enum. Alternatively, the enum itself can be directly referenced. ```php 'phonefield' => (new Phone)->type(1), // becomes 'phonefield' => (new Phone)->type(libphonenumber\PhoneNumberType::from(1)), 'phonefield' => (new Phone)->type(libphonenumber\PhoneNumberType::MOBILE), // or use a string-based rule ``` -------------------------------- ### Eloquent Model Attribute Casting with Custom Country Parameter Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Illustrates how to provide a custom attribute name (e.g., `country_field`) or specific country codes as cast parameters for `RawPhoneNumberCast` and `E164PhoneNumberCast` to dynamically determine the phone number's country. ```php public $casts = [ 'phone_1' => RawPhoneNumberCast::class.':country_field', 'phone_2' => E164PhoneNumberCast::class.':BE', ]; ``` -------------------------------- ### PHP: Laravel Eloquent Search for Phone Numbers Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This snippet provides a Laravel Eloquent query to search for phone numbers across multiple formatted database columns. It uses `WHERE` and `OR WHERE` clauses with `LIKE` to match cleaned search terms against `phone_normalized`, `phone_national`, and `phone_e164` fields. ```php // $search holds the search term User::where(function($query) use ($search) { $query->where('phone_normalized', 'LIKE', preg_replace('/[^0-9]/', '', $search) . '%') ->orWhere('phone_national', 'LIKE', preg_replace('/[^0-9]/', '', $search) . '%') ->orWhere('phone_e164', 'LIKE', preg_replace('/[^+0-9]/', '', $search) . '%') }); ``` -------------------------------- ### Accessing Casted Phone Number Object Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md Shows that after casting, the attribute returns a `PhoneNumber` object, allowing access to its methods, or `null` if the number is invalid or not set. ```php $user->phone // PhoneNumber object or null ``` -------------------------------- ### Enable Lenient Phone Number Validation Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP snippet demonstrates how to enable lenient validation for phone numbers. In lenient mode, only the length of the number is checked, bypassing actual carrier pattern validation, which can be useful for less strict requirements. ```php 'my_input' => 'phone:LENIENT', // 'my_input' => (new Phone)->lenient() ``` -------------------------------- ### Add Phone Validation Message to Laravel Language Files Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP snippet adds a custom validation message for phone numbers to your Laravel application's `validation.php` language files. It ensures a user-friendly error message is displayed when phone number validation fails. ```php 'phone' => 'The :attribute field must be a valid number.' ``` -------------------------------- ### Validate Phone Number by Type (Mobile, Fixed-Line) Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP snippet illustrates how to constrain phone number validation to specific types, such as `mobile` or `fixed_line`. You can append the desired type to the `phone` rule or use the expressive `Phone` rule class with `type()` method, referencing `libphonenumber\PhoneNumberType` constants. ```php 'my_input' => 'phone:mobile', // 'my_input' => (new Phone)->type('mobile') // 'my_input' => (new Phone)->type(libphonenumber\PhoneNumberType::MOBILE) ``` -------------------------------- ### Validate Phone Number Dynamically with Associated Country Field Source: https://github.com/propaganistas/laravel-phone/blob/master/README.md This PHP snippet shows how to validate a phone number based on a dynamically provided country field. If the country field is named `my_input_country` (matching the phone field name with `_country` appended), the validation automatically uses it. Otherwise, a custom country field name can be specified. ```php 'my_input' => 'phone', // 'my_input' => (new Phone) 'my_input_country' => 'required_with:my_input', ``` ```php 'my_input' => 'phone:custom_country_field', // 'my_input' => (new Phone)->countryField('custom_country_field') 'custom_country_field' => 'required_with:my_input', ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.