Skip to main content

AddressResource

JSON resource class for transforming Address models into structured API responses. It can optionally include related geography (country/province/city/district) and versioning information (parent/child/history).

Namespace

JobMetric\Location\Http\Resources\AddressResource

Overview

This resource is typically used when you need to expose an Address entity for an owner model (polymorphic), including normalized location relations and the resolved address fields such as full_address and address_for_letter.

Base Properties

PropertyTypeDescription
idintAddress ID
parent_idint|nullPrevious version ID (when address is versioned)
owner_typestringPolymorphic owner type (FQCN)
owner_idintPolymorphic owner id
addressobject|nullStructured address payload (as stored)
postcodestring|nullPostal code
latstring|nullLatitude
lngstring|nullLongitude
infoarray|nullAdditional info payload
full_addressstringHuman readable full address
address_for_letterstringAddress formatted for letter/printing
ownermixedOwner representation (via owner_resource accessor on the model)

Conditional Properties

PropertyConditionTypeNotes
countrycountry loadedCountryResourcewhenLoaded('country')
provinceprovince loadedProvinceResourcewhenLoaded('province')
citycity loadedCityResourcewhenLoaded('city')
districtdistrict loadedDistrictResourcewhenLoaded('district')
parentparent loadedAddressResourcePrevious version
childchild loadedAddressResourceNext version
historyparent_id !== nullarrayComputed chain of parent addresses (oldest → newest). If parent is not eager loaded, the resource will load it.

Usage Examples

Basic usage

use JobMetric\Location\Http\Resources\AddressResource;
use JobMetric\Location\Models\Address;

$address = Address::find(10);

return AddressResource::make($address);

With geography relations

$address = Address::with(['country', 'province', 'city', 'district'])->find(10);

return AddressResource::make($address);

With versioning chain (parent/child/history)

$address = Address::with(['parent.parent', 'child'])->find(10);

return AddressResource::make($address);

Response Example (simplified)

{
"id": 10,
"parent_id": null,
"owner_type": "App\\\\Models\\\\User",
"owner_id": 1,
"country": { "id": 1, "name": "Iran" },
"province": { "id": 10, "name": "Tehran" },
"city": { "id": 120, "name": "Tehran" },
"district": { "id": 900, "name": "District 1" },
"address": { "street": "Valiasr St", "number": "12" },
"postcode": "1234567890",
"lat": "35.7000",
"lng": "51.4000",
"info": { "name": "John Doe", "mobile": "9120000000" },
"full_address": "Iran, Tehran, Tehran ...",
"address_for_letter": "Iran, Tehran, Tehran\\n...",
"history": [],
"owner": {}
}