定制 rahasistiyak/laravel-geo-data 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rahasistiyak/laravel-geo-data

Composer 安装命令:

composer require rahasistiyak/laravel-geo-data

包简介

A Laravel package providing optimized access to region, country, phone code, city, and currency data

README 文档

README

An optimized Laravel package for accessing region, country, phone code, city, and currency data.

Compatibility

  • PHP: 7.3+, 8.0+
  • Laravel: 6.x, 7.x, 8.x, 9.x, 10.x, 11.x, 12.x

Installation

To install the package, run the following command:

composer require rahasistiyak/laravel-geo-data

The package will be auto-discovered by Laravel.

Publish the Configuration File (if needed)

If you'd like to publish the configuration file for customization, use:

php artisan vendor:publish --tag=geo-data-config

This will publish the configuration file to config/geo-data.php.

Usage

Regions

You can interact with the regions model as follows:

use RahasIstiyak\GeoData\Models\Region;

$regions = Region::all(); // All regions
$dropdown = Region::dropdown(); // ['id', 'name']
$region = Region::byId(1); // By ID
$page = Region::paginate(10, 1); // Paginated results

Countries

You can interact with the countries model as follows:

use RahasIstiyak\GeoData\Models\Country;

$countries = Country::all(); // All countries
$dropdown = Country::dropdown(['id', 'name', 'code']); // Custom fields default if no parameter ['id','name']- if you need more data then send it in parameter
$country = Country::byCode('AF'); // By ISO2/code or ISO3
$country = Country::byId(1); // By ID
$regionCountries = Country::byRegion(3); // By region ID
$page = Country::paginate(10, 1); // Paginated results

Phone Codes

You can interact with the phone codes model as follows:

use RahasIstiyak\GeoData\Models\PhoneCode;

$phoneCodes = PhoneCode::all(); // All phone codes
$dropdown = PhoneCode::dropdown(); // ['country_id', 'phonecode']
$phoneCode = PhoneCode::byCountryId(1); // By country ID
$page = PhoneCode::paginate(10, 1); // Paginated results

Cities

You can interact with the cities model as follows:

use RahasIstiyak\GeoData\Models\City;

$cities = City::getCities('US'); // All cities in the United States
$dropdown = City::getCityDropdown('US'); // Dropdown ['id', 'name'] for the United States
// If you need more data, send parameters like ['id', 'name', 'latitude']
$dropdownWithAdditionalFields = City::getCityDropdown('US', ['id', 'name', 'latitude']); // Additional fields

$city = City::getCityById('US', 1); // Get city with ID 1 in the United States

Currencies

You can interact with the currencies model as follows:

use RahasIstiyak\GeoData\Models\Currency;

$currencies = Currency::all(); // All currencies
$dropdown = Currency::dropdown(); // ['country_id', 'currency', 'currency_name']
$currency = Currency::byCountryId(1); // By country ID
$currency = Currency::byCode('AFN'); // By currency code
$page = Currency::paginate(10, 1); // Paginated results

Configuration

You can edit the configuration in config/geo-data.php. The configuration file includes options for enabling/disabling specific data types and setting cache duration.

return [
    'enabled_data' => [
        'regions' => true,
        'countries' => true,
        'phone_codes' => true,
        'cities' => true,
        'currencies' => true,
    ],
    'cache_ttl' => 1440, // Cache duration in minutes
];

Test Usage

To test the functionality, you can add the following route in your routes/web.php file:

use RahasIstiyak\GeoData\Models\Country;
use RahasIstiyak\GeoData\Models\City;

Route::get('/test-geo', function () {
    return response()->json([
        'countries' => Country::dropdown(),// you can send parameter for custom fields eg: ['id', 'name', 'code']
        'country_af' => Country::byCode('AF'),
        'country_1' => Country::byId(1),
        'cities_us' => City::getCities('US'), // Cities for the United States
        'city_1_us' => City::getCityById('US', 1), // City with ID 1 for US
       
    ]);
});

This will give you a sample response for countries and cities data.

Why This is Optimized

  • Memory Efficiency: Caching with Cache::remember reduces file I/O and memory usage for repeated calls.
  • Code Reusability: The BaseGeoModel class eliminates duplicate logic across models.
  • Large Dataset Handling: The paginate method allows partial loading of large datasets, like cities, to avoid memory overflow.
  • Error Handling: The package validates data files and logs warnings for any missing or corrupted files.
  • Flexibility: Dropdown fields and sorting are customizable to suit different use cases.
  • Performance: Configuration checks are done once during data loading, and cache TTL is configurable for optimized performance.

License

This package is open-source and available under the MIT License.

Contact

For any questions or support, you can reach us at: Email: rahasistiyak.official@gmail.com


### Changes Made:

## [1.0.0] - 2025-06-25
- Initial release with optimized region, country, phone code, city, and currency data access.

## [1.1.0] - 2025-06-26
- Optimized region, country, phone code, city, and currency data access.

## [2.0.0] - 2025-06-26
### Changes Made:
- Updated **Cities** section to reflect the new methods: `getCities()`, `getCityById()`, and `getCityDropdown()`.
- Added example code to demonstrate how these methods can be used.

## [2.1.0] - 2025-06-27
### Changes Made:
- **Added Chunked Caching** for large city datasets to handle **database cache driver** more effectively.
    - Divided the caching process into smaller chunks to avoid exceeding MySQL’s packet size.
    - Introduced `cacheInChunks()` to store the cities data in smaller parts and retrieve them efficiently.
    - This change ensures that caching large city data in the database cache driver doesn't lead to the "MySQL server has gone away" error.
- **Performance Improvement**: Optimized city data retrieval by caching in chunks and merging data from multiple chunks.
- **Database Cache Driver Compatibility**: The chunked caching strategy now ensures compatibility with the database cache driver (`CACHE_DRIVER=database`), preventing large cache payload issues.

## [2.2.0] - 2025-06-27
### Changes Made:
- **Optimized Chunked Caching** for large city datasets to handle **database cache driver** more effectively.
- **Duplicate City Removal**: Remove Duplicate Cities from Dataset.

## [2.3.0] - 2025-06-28
### Changes Made:
- **All Data Return Types Array to Collection** Use $data->name insted of $data['name']

rahasistiyak/laravel-geo-data 适用场景与选型建议

rahasistiyak/laravel-geo-data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「geo」 「currency」 「laravel」 「location」 「country」 「region」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rahasistiyak/laravel-geo-data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rahasistiyak/laravel-geo-data 我们能提供哪些服务?
定制开发 / 二次开发

基于 rahasistiyak/laravel-geo-data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-25