Migrating from PHP

laravel-go-phone Alpha Laravel for Go

Map Laravel Phone’s number helpers, validation rules and Eloquent casts to their Laravel for Go equivalents.

Numbers

PHPGo
new PhoneNumber($raw, ['BE']) or phone($raw, 'BE')phone.New(raw, "BE")
phone($raw, 'BE', 'E164')phone.FormatNumber(raw, phone.E164, "BE")
->lenient()n.WithLeniency(true) returns a new value
->isValid()n.Valid()
->getCountry()n.Country()
->isOfCountry('BE')n.InCountry("BE")
PhoneNumber::isValidCountry('BE')phone.ValidCountry("BE")
->getType()n.Type()
->isOfType('mobile')n.IsType(phone.Mobile)
PhoneNumber::normalizeType('mobile')phone.ParseType("mobile")
PhoneNumber::normalizeFormat('national')phone.ParseFormat("national")
->getRawNumber()n.Raw()
->formatE164()n.E164()
->formatInternational()n.International()
->formatNational()n.National()
->formatRFC3966()n.RFC3966()
->format('NATIONAL')n.Format(phone.National)
->formatForCountry('NL')n.FormatForCountry("NL")
->formatForMobileDialingInCountry('NL')n.FormatForMobileDialing("NL", false)
->equals($other) and ->notEquals($other)n.Equal(other) and n.NotEqual(other)
(string) $numbern.String()
->toJson() or json_encode($number)json.Marshal(n)
serialize and unserializeMarshalText and UnmarshalText, or JSON
->toLibPhoneObject()Parse n.RFC3966() with libphonenumber to keep the extension
PhoneNumber::macro(...)An ordinary Go function taking a phone.Number

Formatting returns a string and an error. JSON represents E.164 as a string, including the leading plus sign. Use phone.New for both operands if you are comparing original text from two places. Equality compares E.164 and ignores extensions.

Validation

PHPGo
'phone' or Rule::phone()phonevalidation.Rule()
'phone:BE,NL' or ->country(['BE', 'NL'])Rule().Country("BE", "NL")
'phone:mobile' or ->type('mobile')Rule().Mobile() or Rule().Type(phone.Mobile)
->notType('voip')Rule().NotType(phone.VoIP)
->fixed_line()Rule().FixedLine()
->lenient()Rule().Lenient()
->international()Rule().International()
->countryField('account.country')Rule().RootCountryField("account.country") for request data
Sibling phone_countryA sibling on the struct declaring the rule
Phone::setDefaultCountry('BE')Rule().DefaultCountries("BE")
validation.phone translationrule.Message(check, message) with a message from the application

Go composes typed rules directly. Laravel supplies field data when it calls Validate. Configure defaults on the rule that uses them. Add rule.Required when a phone is mandatory. A malformed country value produces a validation failure. Laravel Phone issue #284

Casts

PHPGo
E164PhoneNumberCast::class.':BE'Declare a phonemodel.E164 field and construct it with phonemodel.NewE164(raw, "BE")
RawPhoneNumberCast::class.':BE'Declare a *phonemodel.Raw field and call raw.Number("BE") after reading it

Pass country hints in priority order when constructing or resolving a model value. Pass a model's country field explicitly when a national number depends on it. A known region is required when writing E.164 so a subsequent read succeeds. A nil raw field stays NULL, and the text "0" stays text.

Every upstream ability and its status is listed in the coverage matrix.