# Migrating from PHP (laravel-go-phone)

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

- Package: laravel-go-phone (Laravel for Go, alpha), go modules: `github.com/bowerbird-si/laravel-go-phone`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: go.laravel.com v0.2.0 or later; Go: Go 1.27 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-go-phone), [pkg.go.dev](https://pkg.go.dev/github.com/bowerbird-si/laravel-go-phone@v0.1.0), [Changelog](https://oss.bowerbird.si/go-phone/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-go-phone/blob/d3b62daef7b7377a876b692feebe673aa709d898/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-go-phone/blob/d3b62daef7b7377a876b692feebe673aa709d898/SECURITY.md)
- Based on: [Laravel-Phone](https://github.com/Propaganistas/Laravel-Phone): The package this ports: parsing, validation rules, formatting and model storage, following version 6.1.0.; [phonenumbers](https://github.com/nyaruka/phonenumbers): Phone number parsing and the numbering-plan metadata.
- PHP parity: full (30 covered, 21 diverged, 0 planned of 51 abilities; 166 fixtures; follows propaganistas/laravel-phone 6.1.0; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/go-phone/docs/migrating-from-php/
- Source: https://github.com/bowerbird-si/laravel-go-phone/blob/d3b62daef7b7377a876b692feebe673aa709d898/docs/migrating-from-php.md

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

## Numbers

| PHP | Go |
| --- | --- |
| `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) $number` | `n.String()` |
| `->toJson()` or `json_encode($number)` | `json.Marshal(n)` |
| `serialize` and `unserialize` | `MarshalText` 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

| PHP | Go |
| --- | --- |
| `'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_country` | A sibling on the struct declaring the rule |
| `Phone::setDefaultCountry('BE')` | `Rule().DefaultCountries("BE")` |
| `validation.phone` translation | `rule.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](https://github.com/Propaganistas/Laravel-Phone/issues/284)

## Casts

| PHP | Go |
| --- | --- |
| `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](https://github.com/bowerbird-si/laravel-go-phone/blob/main/parity.toml).
