Skip to main content
Addresses store shipping and billing information for customers.

Overview

Addresses belong to Customers and are used to store shipping and billing information. A customer can have multiple addresses, with the ability to mark defaults for both shipping and billing. Addresses are linked to a Country and have an optional state string field for storing state or province information.

Addresses

Lunar\Models\Address
FieldTypeDescription
idprimary key
customer_idforeignId nullableThe related customer
country_idforeignId nullableThe related country
titlestring nullableSalutation e.g. Mr, Mrs, Dr
first_namestring
last_namestring
company_namestring nullable
tax_identifierstring nullable
line_onestringPrimary address line
line_twostring nullable
line_threestring nullable
citystring
statestring nullable
postcodestring nullable
delivery_instructionsstring nullableSpecial delivery notes
contact_emailstring nullable
contact_phonestring nullable
metajson nullable
shipping_defaultbooleanWhether this is the default shipping address
billing_defaultbooleanWhether this is the default billing address
created_attimestamp
updated_attimestamp

Creating an address

The recommended way to create an address is through the customer relationship.
$customer = \Lunar\Models\Customer::find(1);

$address = $customer->addresses()->create([
    'title' => 'Mr',
    'first_name' => 'Tony',
    'last_name' => 'Stark',
    'company_name' => 'Stark Industries',
    'line_one' => '10880 Malibu Point',
    'line_two' => null,
    'line_three' => null,
    'city' => 'Malibu',
    'state' => 'California',
    'postcode' => '90265',
    'country_id' => $country->id,
    'delivery_instructions' => 'Leave at the front gate',
    'contact_email' => 'tony@starkindustries.com',
    'contact_phone' => '555-0123',
    'shipping_default' => true,
    'billing_default' => true,
    'meta' => [
        'type' => 'headquarters',
    ],
]);
An address can also be created directly.
$address = \Lunar\Models\Address::create([
    'customer_id' => $customer->id,
    'first_name' => 'Tony',
    'last_name' => 'Stark',
    'line_one' => '200 Park Avenue',
    'city' => 'New York',
    'postcode' => '10166',
    'country_id' => $country->id,
    'contact_email' => 'tony@starkindustries.com',
    'contact_phone' => '555-0456',
    'shipping_default' => false,
    'billing_default' => false,
]);

Retrieving addresses

// Get all addresses for a customer
$addresses = $customer->addresses;

// Get the default shipping address
$shippingAddress = $customer->addresses()
    ->where('shipping_default', true)
    ->first();

// Get the default billing address
$billingAddress = $customer->addresses()
    ->where('billing_default', true)
    ->first();

Default addresses

Each customer can have one default shipping address and one default billing address. These are controlled by the shipping_default and billing_default boolean fields.
// Set an address as the default shipping address
$address->update([
    'shipping_default' => true,
]);

// Set an address as the default billing address
$address->update([
    'billing_default' => true,
]);
When an address is set as the default, Lunar automatically unsets the previous default for that customer via an observer. There is no need to manually manage this.

Relationships

RelationshipTypeRelated ModelDescription
customerBelongsToLunar\Models\CustomerThe customer who owns this address
countryBelongsToLunar\Models\CountryThe country this address is in
// Get the country for an address
$country = $address->country;

// Get the customer for an address
$customer = $address->customer;