41 lines
1.3 KiB
PHP
41 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class BarangayResource extends JsonResource
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
$boundaryPoints = null;
|
|
if ($this->boundary) {
|
|
$rings = $this->boundary->getGeometries();
|
|
$ring = $rings->first();
|
|
if ($ring) {
|
|
$boundaryPoints = $ring->getGeometries()
|
|
->map(fn ($p) => ['lat' => $p->latitude, 'lng' => $p->longitude])
|
|
->all();
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'psgc_code' => $this->psgc_code,
|
|
'code' => $this->code,
|
|
'name' => $this->name,
|
|
'urban_rural' => $this->urban_rural,
|
|
'population' => $this->population,
|
|
'total_residents' => $this->households_sum_household_size ?? 0,
|
|
'city_municipality_id' => $this->city_municipality_id,
|
|
'centroid' => $this->centroid ? [
|
|
'lat' => $this->centroid->latitude,
|
|
'lng' => $this->centroid->longitude,
|
|
] : null,
|
|
'boundary' => $boundaryPoints,
|
|
'city_municipality' => CityMunicipalityResource::make($this->whenLoaded('cityMunicipality')),
|
|
];
|
|
}
|
|
}
|