# Getting started (laravel-go-phone)

> Use Laravel Go Phone to parse numbers, validate form input and store either E.164 or the original text on a model.

- 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/getting-started/
- Source: https://github.com/bowerbird-si/laravel-go-phone/blob/d3b62daef7b7377a876b692feebe673aa709d898/docs/getting-started.md

Use Laravel Go Phone to parse numbers, validate form input and store either E.164 or the original text on a model.

```shellscript
go get github.com/bowerbird-si/laravel-go-phone
```

| Import suffix | Package | Use |
| --- | --- | --- |
| Root | `phone` | Parse, check and format numbers |
| `/validation` | `phonevalidation` | Validate a Laravel for Go form field |
| `/model` | `phonemodel` | Store raw text or E.164 on a model |

## Parse and format

Pass country hints for national input. The first matching hint is used. A number with an international calling code that can be parsed independently takes precedence.

```go
n := phone.New("0470 12 34 56", "BE")
n.Valid()         // true
n.Country()       // "BE"
n.International() // "+32 470 12 34 56", nil
n.National()      // "0470 12 34 56", nil
n.String()       // "+32470123456"
```

Use `phone.Parse` when you need a parse error immediately. `New` keeps the original text so `String` can display it even when formatting fails. A parseable international number can still fail `Valid`. `WithLeniency(true)` returns a new value that checks possible lengths. A successful check confirms a numbering-plan pattern or possible length. Assignment and reachability require separate verification.

An empty country means the region could not be determined. Global services such as `+800` numbers can have region `001`.

## Validate a field

```go
form := struct {
	Phone   string `json:"phone"`
	Country string `json:"phone_country"`
}{Phone: "0470 12 34 56", Country: "BE"}

errs, err := validation.Validate(ctx, &form,
	rule.Field(&form.Phone, rule.Required, phonevalidation.Rule().Mobile()),
)
```

`errs` contains field failures. `err` contains errors that abort validation, such as a rule configured with both allowed and blocked types.

A nonempty sibling `phone_country` provides the first hint. `Country` supplies additional hints, or `DefaultCountries` supplies them when the rule has no explicit countries. These countries also act as an allowlist. Add `International()` to accept a valid international number outside that list while keeping the hints for national input.

Use `Type`, `NotType`, `Mobile` or `FixedLine` to restrict the number type. `FixedLine` and `Mobile` both match numbers whose plan cannot distinguish the two. `Lenient()` uses the possible-length check and keeps your country and type restrictions. Wrap the rule with `rule.Message(check, message)` to use an application-supplied message. Laravel skips a missing, blank or null value unless a required or other implicit rule applies.

### Nested forms

Define the rule on the nested struct. Laravel then resolves each item's country within that item.

```go
type ContactInput struct {
	Phone   string `json:"phone"`
	Country string `json:"phone_country"`
}

func (c *ContactInput) Rules() validation.Rules {
	return validation.For(c, rule.Field(&c.Phone, phonevalidation.Rule()))
}
```

Use `CountryField("dialing_country")` for another field on that form. Use `RootCountryField("account.country")` for a path from the request root. When validating a struct directly, pass any already-bound root country to the rule explicitly. Laravel provides a relative accessor for bound fields. [Laravel for Go issue #429](https://github.com/laravel/go-framework/issues/429)

## Store numbers on a model

```go
type Contact struct {
	ID      int64           `json:"id"`
	Phone   phonemodel.E164 `json:"phone"`
	Raw     *phonemodel.Raw `json:"raw"`
	Country string          `json:"country"`
}

canonical, err := phonemodel.NewE164(form.Phone, form.Country)
if err != nil {
	return err
}
raw := phonemodel.Raw(form.Phone)
contact := Contact{Phone: canonical, Raw: &raw, Country: form.Country}
return model.Create(ctx, &contact)
```

`Phone.Number()` returns the parsed value. `Raw.Number(contact.Country)` resolves the raw text with the supplied country after the row is read. Check the raw pointer for nil before resolving it.

A zero `E164` or nil `*Raw` writes SQL NULL and encodes as JSON null. An `E164` write requires a region so that the resulting value can be read back. The constructor returns `phone.ErrUnparseable` for text it cannot parse, including a national number without a usable hint. It returns `ErrUnknownRegion` when a parseable value lacks a detectable region in E.164 form. The validation rule checks numbering-plan validity before you construct a model field. Use the raw field when retaining spaces, punctuation or a leading zero matters.
