Laravel for Go Alpha
laravel-go-phone
From a local phone number to a consistent E.164 value
Accept numbers in the formats people use locally. With a Belgian country hint, 0470 12 34 56 becomes +32470123456 for storage and lookup.
Read the docs Source on GitHub
go get github.com/bowerbird-si/laravel-go-phone@v0.1.0- Latest release
- 0.1.0, 2026-12-04
- Maintenance
- Active
- Framework
- go.laravel.com v0.2.0 or later
- Go
- Go 1.27 or later
- Licence
- MIT
Parse and format
Use
phone.Parsewith country hints for national input. Format the result as E.164, international, national or an RFC 3966 telephone URI.Validate Laravel forms
Attach
phonevalidation.Rule()withrule.Fieldand restrict the country or number type. The rule can read a sibling field such asphone_country.Store phone numbers on models
Use
phonemodel.E164to store canonical text and scan it back into a parsed number. Usephonemodel.Rawto keep the text as entered.
PHP parity
Follows propaganistas/laravel-phone 6.1.0 (commit 7433d79ea4b1). Last verified .
- Parity level
- Full
- Abilities
- 51
- Covered
- 30
- Diverged
- 21
- Planned
- 0
- PHP fixtures
- 166
Coverage by ability
-
number.construct· Diverged · CorePhoneNumber::__construct() -
number.country· Covered · Core · 9 testsPhoneNumber::getCountry() -
number.is-of-country· Covered · Core · 4 testsPhoneNumber::isOfCountry() -
number.valid-country· Covered · 6 testsPhoneNumber::isValidCountry() -
number.type· Covered · 5 testsPhoneNumber::getType() -
number.is-of-type· Covered · 3 testsPhoneNumber::isOfType() -
number.normalize-type· Covered · 5 testsPhoneNumber::normalizeType() -
number.format· Covered · 2 testsPhoneNumber::format() -
number.format-international· Covered · 1 testPhoneNumber::formatInternational() -
number.format-national· Covered · Core · 1 testPhoneNumber::formatNational() -
number.format-e164· Covered · Core · 10 testsPhoneNumber::formatE164() -
number.format-rfc3966· Covered · 1 testPhoneNumber::formatRFC3966() -
number.normalize-format· Covered · 2 testsPhoneNumber::normalizeFormat() -
number.format-from-country· Covered · 5 testsPhoneNumber::formatForCountry() -
number.format-mobile-dialing· Covered · 7 testsPhoneNumber::formatForMobileDialingInCountry() -
number.valid· Covered · Core · 7 testsPhoneNumber::isValid() -
number.lenient· Diverged · CorePhoneNumber::lenient() -
number.equals· Covered · 3 testsPhoneNumber::equals() -
number.not-equals· Covered · 3 testsPhoneNumber::notEquals() -
number.raw· Covered · 1 testPhoneNumber::getRawNumber() -
number.libphone-object· DivergedPhoneNumber::toLibPhoneObject() -
number.to-json· DivergedPhoneNumber::toJson() -
number.json-serialize· Covered · 1 testPhoneNumber::jsonSerialize() -
number.serialize· DivergedPhoneNumber::__serialize() -
number.unserialize· DivergedPhoneNumber::__unserialize() -
number.display· Covered · 2 testsPhoneNumber::__toString() -
number.macros· DivergedPhoneNumber::macro() -
helper.phone· Divergedphone() -
rule.data· DivergedRules\Phone::setData() -
rule.validate· Diverged · CoreRules\Phone::validate() -
rule.country· Covered · 3 testsRules\Phone::country() -
rule.country-field· Covered · 4 testsRules\Phone::countryField() -
rule.type· Covered · 3 testsRules\Phone::type() -
rule.not-type· Covered · 2 testsRules\Phone::notType() -
rule.mobile· Covered · 1 testRules\Phone::mobile() -
rule.fixed-line· Covered · 1 testRules\Phone::fixed_line() -
rule.lenient· Covered · 1 testRules\Phone::lenient() -
rule.international· Covered · 2 testsRules\Phone::international() -
rule.parameters· DivergedRules\Phone::setParameters() -
rule.default-country· DivergedRules\Phone::setDefaultCountry() -
rule.alias· Divergedphone validation alias -
rule.factory· Covered · Core · 1 testRule::phone() -
rule.translation· Divergedvalidation.phone message -
provider.discovery· DivergedPhoneServiceProvider registration -
cast.country-hints· DivergedPhoneNumberCast::getPossibleCountries() -
cast.e164.get· DivergedE164PhoneNumberCast::get() -
cast.e164.set· Diverged · CoreE164PhoneNumberCast::set() -
cast.e164.serialize· Covered · 2 testsE164PhoneNumberCast::serialize() -
cast.raw.get· DivergedRawPhoneNumberCast::get() -
cast.raw.set· DivergedRawPhoneNumberCast::set() -
cast.raw.serialize· DivergedRawPhoneNumberCast::serialize()
Differences from PHP (21)
PhoneNumber::__construct()- The shared Go parser accepts more Unicode decimal digits and measures the maximum input length in bytes. PHP rejects the Devanagari example and accepts a padded input that exceeds the Go byte limit. This also affects region detection, validation and formatting of these inputs.
PhoneNumber::lenient()- WithLeniency returns a value copy so changing the mode cannot modify another caller's number.
PhoneNumber::toLibPhoneObject()- The parsed libphonenumber object is private to keep the Go API independent of its backend. Parsing the RFC 3966 output with libphonenumber recovers the calling code, national number and extension.
PhoneNumber::toJson()- Go always emits a JSON string so the E.164 plus sign is preserved. PHP supports JSON_NUMERIC_CHECK, which converts the value to a number.
PhoneNumber::__serialize()- Go text encoding stores the E.164 string. PHP's serialized object format is specific to PHP.
PhoneNumber::__unserialize()- Go restores a number from standard text or JSON encoding. PHP's serialized object format is specific to PHP.
PhoneNumber::macro()- Go has compile-time method sets. Use an ordinary function to add application behavior.
phone()- New and FormatNumber are separate Go functions with distinct return types. PHP selects the return type with an optional format argument.
Rules\Phone::setData()- A non-string dependent country produces a validation failure. PHP throws a TypeError for an array-valued country.
Rules\Phone::validate()- Inputs must be strings. Laravel for Go skips explicit null on a non-implicit rule, so compose the phone rule with Laravel's presence rules when necessary.
Rules\Phone::setParameters()- Use typed builder methods, with field paths distinct from literal countries. Go does not reinterpret options based on a collision with request input keys.
Rules\Phone::setDefaultCountry()- Defaults are configured on an immutable rule value instead of being process-wide mutable state.
phone validation alias- Attach a typed rule with Laravel for Go's rule.Field. It does not evaluate PHP rule strings.
validation.phone message- Pass the application's localized message through Laravel for Go's rule.Message wrapper instead of looking it up in PHP's translator.
PhoneServiceProvider registration- Use the typed rule directly in the form. It does not need Composer discovery or registration with a validator factory.
PhoneNumberCast::getPossibleCountries()- Go model code passes ordered literal hints and sibling country field values explicitly. SQL scanner fields do not have access to other columns.
E164PhoneNumberCast::get()- The stored text 0 is rejected as malformed. PHP treats it as an absent value.
E164PhoneNumberCast::set()- The write rejects malformed or regionless values so that every stored phone can be read back. PHP accepts some parseable regionless writes and treats 0 as absent.
RawPhoneNumberCast::get()- Model code resolves the text with supplied hints after scanning. The stored text 0 raises a conversion error instead of becoming absent.
RawPhoneNumberCast::set()- A nil raw field writes SQL NULL. PHP's raw setter coerces null into empty text.
RawPhoneNumberCast::serialize()- Go marshals the stored raw string or a nil pointer as-is. PHP's raw cast first resolves a phone and treats empty and 0 as absent.
In practice
Convert a Belgian number to E.164
Pass BE as the country hint for the national number.
package phone_test
import (
"fmt"
"github.com/bowerbird-si/laravel-go-phone"
)
func Example() {
number, err := phone.Parse("0470 12 34 56", "BE")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(number)
// Output: +32470123456
}Validate a mobile number in a form
The rule reads phone_country next to phone and checks that the number is mobile.
package phonevalidation_test
import (
"context"
"fmt"
phonevalidation "github.com/bowerbird-si/laravel-go-phone/validation"
"go.laravel.com/validation"
"go.laravel.com/validation/rule"
)
func Example() {
form := struct {
Phone string `json:"phone"`
Country string `json:"phone_country"`
}{Phone: "0470 12 34 56", Country: "BE"}
errs, err := validation.Validate(context.Background(), &form,
rule.Field(&form.Phone, rule.Required, phonevalidation.Rule().Mobile()),
)
fmt.Println(len(errs), err)
// Output: 0 <nil>
}Built on
- Laravel-Phone: The package this ports: parsing, validation rules, formatting and model storage, following version 6.1.0.
- phonenumbers: Phone number parsing and the numbering-plan metadata.
Go modules: github.com/bowerbird-si/laravel-go-phone
· Upstream notices
Documentation
- OverviewParse, validate and format phone numbers in Laravel for Go. Store E.164 values for lookup or keep the original input for display.
- Getting startedUse Laravel Go Phone to parse numbers, validate form input and store either E.164 or the original text on a model.
- Migrating from PHPMap Laravel Phone’s number helpers, validation rules and Eloquent casts to their Laravel for Go equivalents.
- ChangelogWhat changed in each release of laravel-go-phone: additions, changes and fixes.