# Migrating from PHP (laravel-rust-phone)

> Install the Rust crate. The crate names types and methods in Rust style and uses typed errors for calls that can fail.

- Package: laravel-rust-phone (Laravel for Rust, alpha), crates: `laravel-rust-phone`, `laravel-rust-phone-core`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-phone), [crates.io](https://crates.io/crates/laravel-rust-phone/0.1.0), [docs.rs](https://docs.rs/laravel-rust-phone/0.1.0), [Changelog](https://oss.bowerbird.si/phone/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-phone/blob/c3aec2c3f7375e75853ae6a5ef0d1160cd10904d/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-phone/blob/c3aec2c3f7375e75853ae6a5ef0d1160cd10904d/SECURITY.md)
- Based on: [Laravel-Phone](https://github.com/Propaganistas/Laravel-Phone): The package this ports: parsing, validation rules, formatting and model casts, following version 6.1.0.; [phonenumbers-rs](https://github.com/MasonLyons/phonenumbers-rs): Phone number parsing and formatting in Rust (version 0.1.2).; [Google libphonenumber](https://github.com/google/libphonenumber): The numbering-plan data (version 9.0.44).
- PHP parity: full (36 covered, 15 diverged, 0 planned of 51 abilities; 179 fixtures; follows propaganistas/laravel-phone 6.1.0; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/phone/docs/migrating-from-php/
- Source: https://github.com/bowerbird-si/laravel-rust-phone/blob/c3aec2c3f7375e75853ae6a5ef0d1160cd10904d/docs/migrating-from-php.md

Install the [Rust crate](https://oss.bowerbird.si/phone/docs/getting-started/). The crate names types and methods
in Rust style and uses typed errors for calls that can fail.

## Phone numbers

| PHP | Rust |
| --- | --- |
| `new PhoneNumber($number, $countries)` or `phone($number, $countries)` | `phone(number).with_countries(countries)` |
| `phone($number, $countries, $format)` | `phone(number).with_countries(countries).format(format)?` |
| `getCountry()`, `isOfCountry($c)` | `country()`, `is_of_country([c])` |
| `isValidCountry($c)` | `is_valid_country(c)` |
| `isValid()`, `lenient()` | `is_valid()`, `lenient(true)` |
| `getType()`, `isOfType($t)` | `number_type()?`, `is_of_type([t])?` |
| `formatE164()`, `formatInternational()`, `formatNational()`, `formatRFC3966()` | `format_e164()?`, `format_international()?`, `format_national()?`, `format_rfc3966()?` |
| `formatForCountry($c)` | `format_for_country(c)?` |
| `formatForMobileDialingInCountry($c, $f)` | `format_for_mobile_dialing_in_country(c, f)?` |
| `equals($other)`, `notEquals($other)` | `equals(&other)`, `not_equals(&other)` |
| `getRawNumber()`, `toLibPhoneObject()` | `raw_number()`, `parse()?` |
| `(string) $phone` | `phone.to_string()`, with raw text when parsing fails |
| `macro()`, `mixin()` | Rust extension traits |

`"fixed_line".parse::<PhoneNumberType>()` and
`"rfc3966".parse::<PhoneNumberFormat>()` accept PHP names case-insensitively.
Equality compares formatted E.164 values and ignores extensions. Build another
`PhoneNumber` with its own hints to compare national text.

Serde serializes the canonical E.164 string. Convert PHP-serialized objects to
E.164 before importing them. Rust's JSON representation is always a string,
including when PHP's `JSON_NUMERIC_CHECK` option would emit a number.

## Validation

| PHP | Rust |
| --- | --- |
| `new Phone` or `Rule::phone()` | `PhoneRule::new()`, with `into_rule()` for a runtime rules collection |
| `phone:BE,mobile` | `PhoneRule::new().parameters(["BE", "mobile"])` |
| `country(['BE'])`, `countryField('billing.country')` | `countries(["BE"])`, `country_field("billing.country")` |
| `type('mobile')`, `notType('voip')` | `allow_types([PhoneNumberType::Mobile])`, `block_types([PhoneNumberType::Voip])` |
| `mobile()`, `fixed_line()`, `lenient()`, `international()` | The same names on `PhoneRule` |
| `setDefaultCountry('BE')` | `default_countries(["BE"])` on a rule shared through cloning |
| The `validation.phone` language entry | `message(template)`, supplied by your application's catalog |

Attach the rule where the field is declared. This replaces Laravel PHP's
provider registration and string-rule alias. Register rules for collection
members with a nested `Validate` type or `Rules::each`. PHP's wildcard field
declarations do not turn into Laravel Rust field declarations by copying the
string verbatim. An array or object in a country
field fails validation normally. [Laravel Phone issue #284](https://github.com/Propaganistas/Laravel-Phone/issues/284)

## Model columns

Map `E164PhoneNumberCast` to `E164PhoneNumber` and `RawPhoneNumberCast` to
`RawPhoneNumber`. Pass country hints explicitly when you normalize an E.164
write or parse a raw value. This makes the country available before binding
and removes the dependency on assigning one attribute before another.

If you migrate a raw column, SQL NULL and historical empty strings both read
as missing. Rust writes a missing value as SQL NULL. Update queries that use
only `column = ''` if they also need to match new rows whose value is missing.
Raw values serialize as stored text even if the country has not been resolved.

The Rust parser normalizes Unicode decimal digits such as Devanagari, which
the pinned PHP parser rejects. Both parsers limit input to 250 characters after
trimming the ASCII whitespace recognized by PHP.

Submit phone numbers as strings to keep leading zeros. The Rust rule accepts
integer-valued numbers but rejects fractions that PHP might round during conversion.
