[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-8576":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":15,"subscribersCount":15,"size":15,"stars1d":15,"stars7d":16,"stars30d":17,"stars90d":15,"forks30d":15,"starsTrendScore":18,"compositeScore":19,"rankGlobal":10,"rankLanguage":10,"license":20,"archived":21,"fork":21,"defaultBranch":22,"hasWiki":21,"hasPages":21,"topics":23,"createdAt":10,"pushedAt":10,"updatedAt":28,"readmeContent":29,"aiSummary":30,"trendingCount":15,"starSnapshotCount":15,"syncStatus":16,"lastSyncTime":31,"discoverSource":32},8576,"Laravel-Phone","Propaganistas\u002FLaravel-Phone","Propaganistas","Phone number functionality for Laravel","https:\u002F\u002Flaravel-phone.herokuapp.com\u002F",null,"PHP",3013,232,46,0,2,6,1,29.1,"MIT License",false,"master",[24,25,26,27],"laravel","libphonenumber","phone","validation","2026-06-12 02:01:55","# Laravel Phone\n\n[![Tests](https:\u002F\u002Fgithub.com\u002FPropaganistas\u002FLaravel-Phone\u002Factions\u002Fworkflows\u002Ftests.yml\u002Fbadge.svg?branch=master)](https:\u002F\u002Fgithub.com\u002FPropaganistas\u002FLaravel-Phone\u002Factions\u002Fworkflows\u002Ftests.yml)\n[![Latest Stable Version](https:\u002F\u002Fposer.pugx.org\u002Fpropaganistas\u002Flaravel-phone\u002Fv\u002Fstable)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fpropaganistas\u002Flaravel-phone)\n[![Total Downloads](https:\u002F\u002Fposer.pugx.org\u002Fpropaganistas\u002Flaravel-phone\u002Fdownloads)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fpropaganistas\u002Flaravel-phone)\n[![License](https:\u002F\u002Fposer.pugx.org\u002Fpropaganistas\u002Flaravel-phone\u002Flicense)](https:\u002F\u002Fpackagist.org\u002Fpackages\u002Fpropaganistas\u002Flaravel-phone)\n\nAdds phone number functionality to Laravel based on\nthe [PHP port](https:\u002F\u002Fgithub.com\u002Fgiggsey\u002Flibphonenumber-for-php-lite)\nof [libphonenumber by Google](https:\u002F\u002Fgithub.com\u002Fgooglei18n\u002Flibphonenumber).\n\n## Table of Contents\n\n- [Demo](#demo)\n- [Installation](#installation)\n- [Validation](#validation)\n- [Attribute casting](#attribute-casting)\n- [Utility class](#utility-phonenumber-class)\n    - [Formatting](#formatting)\n    - [Number information](#number-information)\n    - [Equality comparison](#equality-comparison)\n- [Database considerations](#database-considerations)\n\n## Demo\n\nCheck out the behavior of this package in the [demo](https:\u002F\u002Flaravel-phone.herokuapp.com).\n\n## Installation\n\nRun the following command to install the latest applicable version of the package:\n\n```bash\ncomposer require propaganistas\u002Flaravel-phone\n```\n\nThe Service Provider gets discovered automatically by Laravel.\n\nIn your languages directory, add an extra translation in every `validation.php` language file:\n\n```php\n'phone' => 'The :attribute field must be a valid number.',\n```\n\n## Validation\n\nUse the `phone` keyword in your validation rules array or use the `Propaganistas\\LaravelPhone\\Rules\\Phone` rule class to\ndefine the rule in an expressive way.\n\nTo put constraints on the allowed originating countries, you can explicitly specify the allowed country codes.\n\n```php\n'my_input'       => 'phone:US,BE',\n\u002F\u002F 'my_input'    => (new Phone)->country(['US', 'BE'])\n```\n\nOr to make things more dynamic, you can also match against another data field holding a country code. For example, to\nrequire a phone number to match the provided country of residence.\nMake sure the country field has the same name as the phone field but with `_country` appended for automatic discovery,\nor provide your custom country field name as a parameter to the validator:\n\n```php\n'my_input'            => 'phone',\n\u002F\u002F 'my_input'         => (new Phone)\n'my_input_country'    => 'required_with:my_input',\n```\n\n```php\n'my_input'            => 'phone:custom_country_field',\n\u002F\u002F 'my_input'         => (new Phone)->countryField('custom_country_field')\n'custom_country_field'  => 'required_with:my_input',\n```\n\nNote: country codes should be [*ISO 3166-1 alpha-2\ncompliant*](http:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FISO_3166-1_alpha-2#Officially_assigned_code_elements).\n\nTo support _any valid internationally formatted_ phone number next to the whitelisted countries, use the `INTERNATIONAL`\nparameter. This can be useful when you're expecting locally formatted numbers from a specific country but also want to\naccept any other foreign number entered properly:\n\n```php\n'my_input'            => 'phone:INTERNATIONAL,BE',\n\u002F\u002F 'my_input'         => (new Phone)->international()->country('BE')\n```\n\nTo specify constraints on the number type, append the allowed types to the parameters, e.g.:\n\n```php\n'my_input'       => 'phone:mobile',\n\u002F\u002F 'my_input'    => (new Phone)->type('mobile')\n\u002F\u002F 'my_input'    => (new Phone)->type(libphonenumber\\PhoneNumberType::MOBILE)\n```\n\nThe most common types are `mobile` and `fixed_line`, but feel free to use any of the types\ndefined [here](https:\u002F\u002Fgithub.com\u002Fgiggsey\u002Flibphonenumber-for-php-lite\u002Fblob\u002Fmaster\u002Fsrc\u002FPhoneNumberType.php).\n\nPrepend a type with an exclamation mark to blacklist it instead. Note that you can never use whitelisted *and*\nblacklisted types at the same time.\n\n```php\n'my_input'       => 'phone:!mobile',\n\u002F\u002F 'my_input'    => (new Phone)->notType('mobile')\n\u002F\u002F 'my_input'    => (new Phone)->notType(libphonenumber\\PhoneNumberType::MOBILE)\n```\n\nYou can also enable lenient validation by using the `LENIENT` parameter.\nWith leniency enabled, only the length of a number is checked instead of actual carrier patterns.\n\n```php\n'my_input'       => 'phone:LENIENT',\n\u002F\u002F 'my_input'    => (new Phone)->lenient()\n```\n\n## Attribute casting\n\nTwo cast classes are provided for automatic casting of Eloquent model attributes:\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Propaganistas\\LaravelPhone\\Casts\\RawPhoneNumberCast;\nuse Propaganistas\\LaravelPhone\\Casts\\E164PhoneNumberCast;\n\nclass User extends Model\n{\n    public $casts = [\n        'phone_1' => RawPhoneNumberCast::class.':BE',\n        'phone_2' => E164PhoneNumberCast::class.':BE',\n    ];\n}\n```\n\nBoth classes automatically cast the database value to a PhoneNumber object for further use in your application.\n\n```php\n$user->phone \u002F\u002F PhoneNumber object or null\n```\n\nWhen setting a value, they both accept a string value or a PhoneNumber object.\nThe `RawPhoneNumberCast` mutates the database value to the raw input number, while the `E164PhoneNumberCast` writes a\nformatted E.164 phone number to the database.\n\nIn case of `RawPhoneNumberCast`, the cast needs to be hinted about the phone country in order to properly parse the raw\nnumber into a phone object.\nIn case of `E164PhoneNumberCast` and the value to be set is not already in some international format, the cast needs to\nbe hinted about the phone country in order to properly mutate the value.\n\nBoth classes accept cast parameters in the same way:\n\n1. When a similar named attribute exists, but suffixed with `_country` (e.g. phone_country), the cast will detect and\n   use it automatically.\n2. Provide another attribute's name as a cast parameter\n3. Provide one or several country codes as cast parameters\n\n```php\npublic $casts = [\n    'phone_1' => RawPhoneNumberCast::class.':country_field',\n    'phone_2' => E164PhoneNumberCast::class.':BE',\n];\n```\n\n**Important note:** Both casts expect __valid__ phone numbers in order to smoothly convert from\u002Fto PhoneNumber objects.\nPlease validate phone numbers before setting them on a model. Refer to the [validation documentation](#validation) to\nlearn how to validate phone numbers.\n\n#### ⚠️ Attribute assignment and `E164PhoneNumberCast`\n\nDue to the nature of `E164PhoneNumberCast` a valid country attribute is expected if the number is not passed in\ninternational format. Since Laravel applies casts instantly when setting an attribute, be sure to set the country\nattribute _before_ setting the phone number attribute. Otherwise `E164PhoneNumberCast` will encounter an empty country\nvalue and throw an unexpected exception.\n\n```php\n\u002F\u002F Wrong\n$model->fill([\n    'phone' => '012 34 56 78',\n    'phone_country' => 'BE',\n]);\n\n\u002F\u002F Correct\n$model->fill([\n    'phone_country' => 'BE',\n    'phone' => '012 34 56 78',\n]);\n\n\u002F\u002F Wrong\n$model->phone = '012 34 56 78';\n$model->phone_country = 'BE';\n\n\u002F\u002F Correct\n$model->phone_country = 'BE';\n$model->phone = '012 34 56 78';\n```\n\n## Utility PhoneNumber class\n\nA phone number can be wrapped in the `Propaganistas\\LaravelPhone\\PhoneNumber` class to enhance it with useful utility\nmethods. It's safe to directly reference these objects in views or when saving to the database as they will degrade\ngracefully to the E.164 format.\n\n```php\nuse Propaganistas\\LaravelPhone\\PhoneNumber;\n\n(string) new PhoneNumber('+3212\u002F34.56.78');                \u002F\u002F +3212345678\n(string) new PhoneNumber('012 34 56 78', 'BE');            \u002F\u002F +3212345678\n```\n\nAlternatively you can use the `phone()` helper function. It returns a `Propaganistas\\LaravelPhone\\PhoneNumber` instance\nor the formatted string if `$format` was provided:\n\n```php\nphone('+3212\u002F34.56.78');                \u002F\u002F PhoneNumber instance\nphone('012 34 56 78', 'BE');            \u002F\u002F PhoneNumber instance\nphone('012 34 56 78', 'BE', $format);   \u002F\u002F string\n```\n\n### Formatting\n\nA PhoneNumber can be formatted in various ways:\n\n```php\n$phone = new PhoneNumber('012\u002F34.56.78', 'BE');\n\n$phone->format($format);       \u002F\u002F See libphonenumber\\PhoneNumberFormat\n$phone->formatE164();          \u002F\u002F +3212345678\n$phone->formatInternational(); \u002F\u002F +32 12 34 56 78\n$phone->formatRFC3966();       \u002F\u002F tel:+32-12-34-56-78\n$phone->formatNational();      \u002F\u002F 012 34 56 78\n\n\u002F\u002F Formats so the number can be called straight from the provided country.\n$phone->formatForCountry('BE'); \u002F\u002F 012 34 56 78\n$phone->formatForCountry('NL'); \u002F\u002F 00 32 12 34 56 78\n$phone->formatForCountry('US'); \u002F\u002F 011 32 12 34 56 78\n\n\u002F\u002F Formats so the number can be clicked on and called straight from the provided country using a cellphone.\n$phone->formatForMobileDialingInCountry('BE'); \u002F\u002F 012345678\n$phone->formatForMobileDialingInCountry('NL'); \u002F\u002F +3212345678\n$phone->formatForMobileDialingInCountry('US'); \u002F\u002F +3212345678\n```\n\n### Number information\n\nGet some information about the phone number:\n\n```php\n$phone = new PhoneNumber('012 34 56 78', 'BE');\n\n$phone->getType();              \u002F\u002F libphonenumber\\PhoneNumberType::FIXED_LINE\n$phone->isOfType('fixed_line'); \u002F\u002F true    (or use $phone->isOfType(libphonenumber\\PhoneNumberType::FIXED_LINE) )\n$phone->getCountry();           \u002F\u002F 'BE'\n$phone->isOfCountry('BE');      \u002F\u002F true\n```\n\n### Equality comparison\n\nCheck if a given phone number is (not) equal to another one:\n\n```php\n$phone = new PhoneNumber('012 34 56 78', 'BE');\n\n$phone->equals('012\u002F34.56.76', 'BE')       \u002F\u002F true\n$phone->equals('+32 12 34 56 78')          \u002F\u002F true\n$phone->equals( $anotherPhoneObject )      \u002F\u002F true\u002Ffalse\n\n$phone->notEquals('045 67 89 10', 'BE')    \u002F\u002F true\n$phone->notEquals('+32 45 67 89 10')       \u002F\u002F true\n$phone->notEquals( $anotherPhoneObject )   \u002F\u002F true\u002Ffalse\n```\n\n## Database considerations\n\n> Disclaimer: Phone number handling is quite different in each application. The topics mentioned below are therefore\n> meant as a set of thought starters; support will **not** be provided.\n\nStoring phone numbers in a database has always been a speculative topic and there's simply no silver bullet. It all\ndepends on your application's requirements. Here are some things to take into account, along with an implementation\nsuggestion. Your ideal database setup will probably be a combination of some of the pointers detailed below.\n\n### Uniqueness\n\nThe E.164 format globally and uniquely identifies a phone number across the world. It also inherently implies a specific\ncountry and can be supplied as-is to the `phone()` helper.\n\nYou'll need:\n\n* One column to store the phone number\n* To format the phone number to E.164 before persisting it\n\nExample:\n\n* User input = `012\u002F45.65.78`\n* Database column\n    * `phone` (varchar) = `+3212456578`\n\n### Presenting the phone number the way it was inputted\n\nIf you store formatted phone numbers the raw user input will unretrievably get lost. It may be beneficial to present\nyour users with their very own inputted phone number, for example in terms of improved user experience.\n\nYou'll need:\n\n* Two columns to store the raw input and the correlated country\n\nExample:\n\n* User input = `012\u002F34.56.78`\n* Database columns\n    * `phone` (varchar) = `012\u002F34.56.78`\n    * `phone_country` (varchar) = `BE`\n\n### Supporting searches\n\nSearching through phone numbers can quickly become ridiculously complex and will always require deep understanding of\nthe context and extent of your application. Here's _a_ possible approach covering quite a lot of \"natural\" use cases.\n\nYou'll need:\n\n* Three additional columns to store searchable variants of the phone number:\n    * Normalized input (raw input with all non-alpha characters stripped)\n    * National formatted phone number (with all non-alpha characters stripped)\n    * E.164 formatted phone number\n* Probably a `saving()` observer (or equivalent) to prefill the variants before persistence\n* An extensive search query utilizing the searchable variants\n\nExample:\n\n* User input = `12\u002F34.56.78`\n* Observer method:\n  ```php\n  public function saving(User $user)\n  {\n      if ($user->isDirty('phone') && $user->phone) {\n          $user->phone_normalized = preg_replace('\u002F[^0-9]\u002F', '', $user->phone);\n          $user->phone_national = preg_replace('\u002F[^0-9]\u002F', '', phone($user->phone, $user->phone_country)->formatNational());\n          $user->phone_e164 = phone($user->phone, $user->phone_country)->formatE164();\n      }\n  }\n  ```\n* Database columns\n    * `phone_normalized` (varchar) = `12345678`\n    * `phone_national` (varchar) = `012345678`\n    * `phone_e164` (varchar) = `+3212345678`\n* Search query:\n  ```php\n  \u002F\u002F $search holds the search term\n  User::where(function($query) use ($search) {\n    $query->where('phone_normalized', 'LIKE', preg_replace('\u002F[^0-9]\u002F', '', $search) . '%')\n          ->orWhere('phone_national', 'LIKE', preg_replace('\u002F[^0-9]\u002F', '', $search) . '%')\n          ->orWhere('phone_e164', 'LIKE', preg_replace('\u002F[^+0-9]\u002F', '', $search) . '%')\n  });\n  ```\n","Laravel-Phone 是一个为 Laravel 框架提供电话号码处理功能的扩展包。它基于 Google 的 libphonenumber 库，支持电话号码验证、格式化以及信息提取等功能，能够自动识别并转换不同国家\u002F地区的电话号码格式。此外，该库还提供了便捷的属性转换和实用工具类，便于开发者在应用中轻松集成电话号码相关操作。适用于需要处理多国电话号码输入与存储的场景，如国际化的用户注册系统或客服平台等。","2026-06-11 03:18:40","top_language"]