edeesonopina/laravel-psgc-api
Composer 安装命令:
composer require edeesonopina/laravel-psgc-api
包简介
Philippine Standard Geographic Code (PSGC) API package for Laravel - Official PSA data with import/export functionality
关键字:
README 文档
README
A comprehensive REST API for the Philippine Standard Geographic Code (PSGC) data, providing access to official administrative divisions of the Philippines including regions, provinces, cities/municipalities, and barangays.
📦 Laravel Package (Recommended)
For existing Laravel projects - Add PSGC functionality to your project:
composer require edeesonopina/laravel-psgc-api
Quick Setup
# Install the package composer require edeesonopina/laravel-psgc-api # Run setup script (optional - handles everything automatically) node vendor/edeesonopina/laravel-psgc-api/scripts/install-package.js # Start using the API php artisan serve # API available at: http://localhost:8000/api/v1/regions
Manual Setup
# Run migrations php artisan migrate # Import PSGC data php artisan psgc:import --regions=database/psgc/data/regions.csv --provinces=database/psgc/data/provinces.csv --city_municipalities=database/psgc/data/city_municipalities.csv --barangays=database/psgc/data/barangays.csv
Customization Options
Publish Controllers (to customize API logic):
php artisan vendor:publish --provider="EdeesonOpina\PsgcApi\Providers\PsgcApiServiceProvider" --tag="psgc-controllers"
Publish Routes (to customize API endpoints):
php artisan vendor:publish --provider="EdeesonOpina\PsgcApi\Providers\PsgcApiServiceProvider" --tag="psgc-routes"
Publish Models (to add custom relationships):
php artisan vendor:publish --provider="EdeesonOpina\PsgcApi\Providers\PsgcApiServiceProvider" --tag="psgc-models"
Package Documentation: PACKAGE_README.md | Installation Guide
🚀 Features
- Official PSGC Data: Uses the latest official PSGC data from the Philippine Statistics Authority (PSA) 2025 2Q
- Complete Coverage: All 18 regions, 115 provinces, 1,656 cities/municipalities, and 42,011 barangays
- RESTful API: Clean, intuitive REST endpoints with pagination
- Laravel Framework: Built with Laravel 11 for reliability and performance
- Public Access: Free API for public use and digital integration
📋 Requirements
System Requirements
- PHP: 8.1 or higher
- Laravel: 9.x, 10.x, 11.x, or 12.x
- Database: MySQL, PostgreSQL, SQLite, or SQL Server
- Node.js: 16.x or higher (for installation script)
PHP Extensions
ext-pdo- Database connectivityext-mbstring- String handlingext-openssl- Security featuresext-tokenizer- Code parsingext-xml- XML processingext-ctype- Character type checkingext-json- JSON processingext-bcmath- Arbitrary precision mathematics
Laravel Package Requirements
illuminate/support: ^9.0|^10.0|^11.0|^12.0illuminate/database: ^9.0|^10.0|^11.0|^12.0illuminate/console: ^9.0|^10.0|^11.0|^12.0
📊 Data Statistics
- Regions: 18
- Provinces: 115
- Cities/Municipalities: 1,656
- Barangays: 42,011
- Total Records: 43,800
🔗 API Endpoints
Base URL
http://localhost:8000/api/v1
Important: All endpoint parameters use database IDs (not PSGC codes). This makes the API more developer-friendly and consistent with standard REST practices.
Regions
- GET
/regions- List all regions - GET
/regions/{id}- Get specific region by database ID
Query Parameters:
q- Search by name or code (e.g.,?q=manila)
Provinces
- GET
/provinces- List all provinces - GET
/provinces/{id}- Get specific province by database ID
Query Parameters:
q- Search by name or code (e.g.,?q=manila)region_id- Filter by region ID (e.g.,?region_id=19)
Cities/Municipalities
- GET
/city-municipalities- List all cities and municipalities - GET
/city-municipalities/{id}- Get specific city/municipality by database ID
Query Parameters:
q- Search by name or code (e.g.,?q=manila)province_id- Filter by province ID (e.g.,?province_id=86)region_id- Filter by region ID (e.g.,?region_id=19)type- Filter by type:CityorMunicipality(e.g.,?type=City)limit- Limit results (max 1000, e.g.,?limit=50)
Barangays
- GET
/barangays- List all barangays - GET
/barangays/{id}- Get specific barangay by database ID
Query Parameters:
q- Search by name or code (e.g.,?q=malate)city_municipality_id- Filter by city/municipality ID (e.g.,?city_municipality_id=44)province_id- Filter by province ID (e.g.,?province_id=86)region_id- Filter by region ID (e.g.,?region_id=19)limit- Limit results (max 1000, e.g.,?limit=100)
📝 Response Format
All endpoints return JSON responses directly (no pagination wrapper):
[
{
"id": 19,
"code": "1300000000",
"name": "National Capital Region (NCR)",
"short_name": "13",
"island_group": "",
"status": "active",
"deleted_at": null,
"created_at": "2025-10-06T05:14:41.000000Z",
"updated_at": "2025-10-06T05:14:41.000000Z"
}
]
🚀 API Examples
Get All Regions
curl http://localhost:8000/api/v1/regions
Get Specific Region
curl http://localhost:8000/api/v1/regions/19
Get Provinces in NCR
curl http://localhost:8000/api/v1/provinces?region_id=19
Get Cities in a Province
curl http://localhost:8000/api/v1/city-municipalities?province_id=86&limit=10
Get Barangays in Manila
curl http://localhost:8000/api/v1/barangays?city_municipality_id=44&limit=20
Search for Barangays
curl http://localhost:8000/api/v1/barangays?q=malate&limit=5
Get All Cities (Limited)
curl http://localhost:8000/api/v1/city-municipalities?type=City&limit=50
🔄 Migration Guide (v1.0.0 → v1.1.0)
Breaking Changes
⚠️ Important: v1.1.0 contains breaking changes. Existing applications using v1.0.0 will need updates.
1. Endpoint Parameters Changed
# v1.0.0 (Old) GET /api/v1/regions/{code} # Used PSGC codes GET /api/v1/provinces?region_code=1300000000 # v1.1.0 (New) GET /api/v1/regions/{id} # Uses database IDs GET /api/v1/provinces?region_id=19
2. Response Format Changed
// v1.0.0 (Old) { "table": "regions", "rows": [...], "pagination": {...} } // v1.1.0 (New) [...] // Direct array, no wrapper
3. New Features Added
- Limit Parameter:
?limit=50for cities and barangays - Publishable Components: Controllers, routes, and models can be customized
- Improved Performance: No pagination overhead for small datasets
Migration Steps
-
Update Package Version:
composer require edeesonopina/laravel-psgc-api:^1.1.0
-
Update API Calls:
- Replace
{code}with{id}in URLs - Replace
region_codewithregion_idin parameters - Update response parsing (remove pagination wrapper)
- Replace
-
Test Your Application:
- Verify all API endpoints work with new format
- Update frontend code to handle direct JSON arrays
Staying on v1.0.0
If you prefer to stay on the stable v1.0.0:
composer require edeesonopina/laravel-psgc-api:1.0.0
🛠️ Standalone Project Setup
For new Laravel projects - Clone and set up from scratch:
Prerequisites
- PHP 8.2+
- Composer
- MySQL/SQLite
- Node.js (for data conversion)
Installation Steps
-
Clone the repository
git clone https://github.com/EdeesonOpina/laravel-psgc-api.git cd laravel-psgc-api -
Install PHP dependencies
composer install
-
Install Node.js dependencies
npm install
-
Environment setup
cp .env.example .env php artisan key:generate
-
Database setup
php artisan migrate
-
Import PSGC data
# Generate CSV files from official PSGC data node scripts/convert_psgc_to_csv.js # Import the data php artisan psgc:import --regions=database/psgc/data/regions.csv --provinces=database/psgc/data/provinces.csv --city_municipalities=database/psgc/data/city_municipalities.csv --barangays=database/psgc/data/barangays.csv
-
Start the server
php artisan serve --port=8000
📋 Data Import & Export Commands
Import Command
The psgc:import command provides several options:
php artisan psgc:import [options] Options: --regions=FILE Path to regions CSV file --provinces=FILE Path to provinces CSV file --city_municipalities=FILE Path to city/municipalities CSV file --barangays=FILE Path to barangays CSV file --truncate Truncate all PSGC tables before import --dry-run Validate only (no DB commit)
Examples
# Import all data php artisan psgc:import --regions=database/psgc/data/regions.csv --provinces=database/psgc/data/provinces.csv --city_municipalities=database/psgc/data/city_municipalities.csv --barangays=database/psgc/data/barangays.csv # Dry run (validate only) php artisan psgc:import --regions=database/psgc/data/regions.csv --dry-run # Truncate and re-import php artisan psgc:import --truncate --regions=database/psgc/data/regions.csv --provinces=database/psgc/data/provinces.csv --city_municipalities=database/psgc/data/city_municipalities.csv --barangays=database/psgc/data/barangays.csv
Export Command
The psgc:export command allows you to export data from the database back to CSV or JSON files:
php artisan psgc:export [options] Options: --output=DIR Output directory (default: exports) --regions Export regions only --provinces Export provinces only --city_municipalities Export cities/municipalities only --barangays Export barangays only --all Export all data (default) --format=FORMAT Export format: csv or json (default: csv) --status=STATUS Filter by status: active, inactive, or all (default: active)
Export Examples
# Export all data to CSV php artisan psgc:export # Export all data to JSON php artisan psgc:export --format=json # Export only regions to custom directory php artisan psgc:export --regions --output=backup # Export provinces and cities to JSON php artisan psgc:export --provinces --city_municipalities --format=json # Export all data including inactive records php artisan psgc:export --status=all # Export to specific directory php artisan psgc:export --output=my-exports --format=csv
🗄️ Database Schema
Regions Table
id- Primary keycode- PSGC code (10 characters)name- Region nameshort_name- Short region codeisland_group- Island group (Luzon/Visayas/Mindanao)status- Status (active/inactive)deleted_at- Soft delete timestampcreated_at- Creation timestampupdated_at- Update timestamp
Provinces Table
id- Primary keycode- PSGC code (10 characters)name- Province nameregion_id- Foreign key to regions tableold_name- Previous name (if any)status- Status (active/inactive)deleted_at- Soft delete timestampcreated_at- Creation timestampupdated_at- Update timestamp
City/Municipalities Table
id- Primary keycode- PSGC code (10 characters)name- City/Municipality nameprovince_id- Foreign key to provinces tableregion_id- Foreign key to regions tabletype- Type (City/Municipality)income_class- Income classificationurban_rural- Urban/Rural classificationold_name- Previous name (if any)status- Status (active/inactive)deleted_at- Soft delete timestampcreated_at- Creation timestampupdated_at- Update timestamp
Barangays Table
id- Primary keycode- PSGC code (10 characters)name- Barangay namecity_municipality_id- Foreign key to city_municipalities tableprovince_id- Foreign key to provinces tableregion_id- Foreign key to regions tableold_name- Previous name (if any)status- Status (active/inactive)deleted_at- Soft delete timestampcreated_at- Creation timestampupdated_at- Update timestamp
🔄 Data Updates
The PSGC data is updated quarterly by the Philippine Statistics Authority. To update your local data:
-
Download latest data
npm install @jobuntux/psgc@latest
-
Regenerate CSV files
node scripts/convert_psgc_to_csv.js
-
Import updated data
php artisan psgc:import --truncate --regions=database/psgc/data/regions.csv --provinces=database/psgc/data/provinces.csv --city_municipalities=database/psgc/data/city_municipalities.csv --barangays=database/psgc/data/barangays.csv
🙏 Acknowledgments
This project would not be possible without the following contributors and resources:
Data Source & Libraries
- @jobuntux/psgc - TypeScript-ready NPM package providing up-to-date PSGC data, updated quarterly in sync with PSA's official releases
- Philippine Statistics Authority (PSA) - Official source of PSGC data and classifications
- Laravel Framework - Robust PHP framework for building reliable APIs
Development Team
- Edeeson Opina - Full Stack Web Developer & Project Lead
- Website: https://edeesonopina.vercel.app/
- Responsible for API architecture, data integration, and system implementation
Special Thanks
- The Philippine Statistics Authority for maintaining and providing official PSGC data
- The open-source community for tools and libraries that made this project possible
- All contributors who help maintain the PSGC data accuracy and accessibility
🤝 Contributing
This API is designed for public use and digital integration. Contributions are welcome!
📄 License
This project is open source and available under the MIT License.
📞 Support
For questions or support, please open an issue in the repository or contact the development team.
🏛️ Data Source
This API uses official PSGC data from the Philippine Statistics Authority (PSA):
- Source: PSA PSGC 2025 2Q
- Update Frequency: Quarterly
- Official Website: psa.gov.ph/classification/psgc
- Data Package: Powered by @jobuntux/psgc
Note: This API provides the latest official PSGC data for digital integration and public use. The data is maintained according to PSA standards and updated regularly to reflect administrative changes in the Philippines.
edeesonopina/laravel-psgc-api 适用场景与选型建议
edeesonopina/laravel-psgc-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 322 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「psa」 「cities」 「regions」 「philippines」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 edeesonopina/laravel-psgc-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 edeesonopina/laravel-psgc-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 edeesonopina/laravel-psgc-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SOAP client wrapper for talking to Autotask's Web Service
A PSR-7 compatible library for making CRUD API endpoints
Alfabank REST API integration
Laravel package for Accurate Online API integration.
ConnectWise PSA REST API Client
A lightweight plain-PHP framework for database-backed CRUD APIs.
统计信息
- 总下载量: 322
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 41
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-06