Migrating from PHP

laravel-rust-phone Alpha Laravel for Rust

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

Phone numbers

PHPRust
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) $phonephone.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

PHPRust
new Phone or Rule::phone()PhoneRule::new(), with into_rule() for a runtime rules collection
phone:BE,mobilePhoneRule::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 entrymessage(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

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.