insidePhilippinesBoundingBox($latitude, $longitude)) { return null; } $key = sprintf('geo:resolve:%.6f:%.6f', $latitude, $longitude); $barangayId = Cache::remember($key, $this->cacheTtlSeconds, function () use ($latitude, $longitude) { $row = DB::table('barangays') ->whereNull('deleted_at') ->whereNotNull('boundary') ->whereRaw( 'ST_Contains(boundary, ST_SRID(POINT(?, ?), 4326))', [$longitude, $latitude], ) ->select('id') ->first(); return $row?->id; }); if (! $barangayId) { return null; } return Barangay::with('cityMunicipality.province.region')->find($barangayId); } private function insidePhilippinesBoundingBox(float $latitude, float $longitude): bool { // Rough bounds for the Philippines archipelago. return $latitude >= 4.5 && $latitude <= 21.5 && $longitude >= 116.0 && $longitude <= 127.0; } }