Skip to main content

CountryResource

JSON resource class for transforming Country models into structured API responses. This resource provides a consistent format for country data with conditional relation loading.

Namespace

JobMetric\Location\Http\Resources\CountryResource

Overview

The CountryResource transforms Country model instances into structured JSON responses. It includes:

  • Core country properties
  • Conditional relation loading (provinces/cities/districts)

Base Properties

The resource always includes these properties:

PropertyTypeDescription
idintCountry ID
namestringCountry name
flagstring|nullFlag key/filename
mobile_prefixstring|nullCalling code (may be formatted by model accessor)
validationarray|nullMobile validation regex patterns
statusboolEnabled/disabled

Conditional Properties

These properties are only included when their relations are loaded:

  • provincesProvinceResource::collection(...)
  • citiesCityResource::collection(...)
  • districtsDistrictResource::collection(...)

Usage Examples

Basic usage

use JobMetric\Location\Http\Resources\CountryResource;
use JobMetric\Location\Models\Country;

$country = Country::find(1);

return CountryResource::make($country);

With relations

$country = Country::with(['provinces'])->find(1);

return CountryResource::make($country);

Response Example (simplified)

{
"id": 1,
"name": "Iran",
"flag": "ir.svg",
"mobile_prefix": "+98",
"validation": ["/^9\\d{9}$/"],
"status": true,
"provinces": []
}