Skip to main content

Overview

Lunar ships with a full dataset of countries and states that can be imported into your database. These are used across the system for Addresses, shipping zone configuration, tax calculations, and more.

Countries

Lunar\Models\Country
FieldDescription
id
nameFull country name e.g. United States
iso3ISO 3166-1 alpha-3 code e.g. USA
iso2ISO 3166-1 alpha-2 code e.g. US
phonecodeInternational dialling code e.g. 1
capitalCapital city
currencyCurrency code e.g. USD
nativeNative name of the country
emojiCountry flag emoji
emoji_uCountry flag unicode
created_at
updated_at

Retrieving countries

use Lunar\Models\Country;

// Find a country by ISO code
$country = Country::where('iso2', 'US')->first();

// Get all countries
$countries = Country::all();

// Get a country with its states
$country = Country::with('states')->where('iso2', 'GB')->first();

Relationships

RelationshipType
statesHasMany

States

States (also known as provinces or regions) belong to a country. They are useful for tax calculations and shipping zone configuration.
Lunar\Models\State
FieldDescription
id
country_idThe related country
nameFull state name e.g. California
codeState code e.g. CA
created_at
updated_at

Retrieving states

use Lunar\Models\State;

// Get all states for a country
$states = $country->states;

// Find a state by code within a country
$state = State::where('country_id', $country->id)
    ->where('code', 'CA')
    ->first();

Relationships

RelationshipType
countryBelongsTo

Importing Data

Country and state data is provided by the countries-states-cities-database. Use the following Artisan command to import the data into your database.
php artisan lunar:import:address-data
This command should be run after installing Lunar and after any updates that may include new country or state data.