“Which districts belong to Dhaka?” was one of the first queries I showed when I wrote about my Bangladesh Geocode package in 2019. With the release of my Go module, I can ask the same question in a Laravel Go handler and get back a slice of district structs. The records keep the IDs and names used by the PHP package, including the Bangla names you can show in an address form. Original PHP package · Go module

Laravel released the first beta of Laravel Go on December 2. It builds on Go’s standard net/http package and passes database work through context.Context. If you already write Go, the result of a Laravel model query will look familiar. You get a typed value and an error, and you can pass the request context all the way to the database. Laravel Go release announcement

Load the data into a Go app

The module targets Go 1.27 and Laravel Go v0.1.0. From your application directory, install the module with Go.

go get github.com/devfaysal/laravel-bangladesh-geocode-go@latest

In bootstrap/app.go, import the module as geocode and add geocode.Package{} to the Packages slice in your existing laravel.Options. Configure the app’s database, then run the setup command.

go run . BangladeshGeocode:setup

Setup imports 8 divisions, 64 districts, 495 upazilas, 4,548 unions, and 1,357 post offices. One detail matters if you are building a postal lookup. A post office belongs to a district and carries its own thana name and four-digit postal code. For a district dropdown, follow the division’s districts. For a postal lookup, start with the district’s post offices.

Ask for Dhaka’s districts

Here is the query inside a handler or helper that already has a ctx. Alongside the standard context package, import go.laravel.com/model and github.com/devfaysal/laravel-bangladesh-geocode-go/models.

func districtsOfDhaka(ctx context.Context) ([]models.District, error) {
    dhaka, err := model.Query[models.Division]().WhereEq("name", "Dhaka").First(ctx)
    if err != nil {
        return nil, err
    }

    return model.Related[models.District](&dhaka, "districts").
        OrderBy("name").
        Get(ctx)
}

The example app uses that same parent-child relationship for its bilingual address form. Open /locations and choose a division. To see exactly what fills the next dropdown, open /locations/divisions/1/districts in a second tab. Each result contains just id, name, and bn_name. The form can display either spelling and keep the same ID for the next request. The repository includes the MySQL setup and the commands to run it locally. Run the Go example app