nerdsnipe/laravel-countries 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

nerdsnipe/laravel-countries

最新稳定版本:v1.2.0

Composer 安装命令:

composer require nerdsnipe/laravel-countries

包简介

Used to add country, state, city selectors to any laravel view. Provides all cities and states for 251 countries

README 文档

README

Latest Version on Packagist Total Downloads MIT Licensed

A Laravel package for working with countries, states, provinces, and cities. Use this package to easily add a dynamic country, state and cities select element series to your Laravel application. All required data is available in the included JSON files which are NOT imported to your database.

Requirements

  • PHP >= 8.1
  • Laravel >= 9.0

Installation

You can install the package via composer:

composer require nerdsnipe/laravel-countries

You must publish the package config file.

php artisan vendor:publish --provider="NerdSnipe\LaravelCountries\LaravelCountriesServiceProvider" --tag="config"

This will publish the config/laravel-countries.php file.

Set the script_stack and style_stack to reflect where to place script and css elements within your view or layout.

return [
    
    /**
     * The default country to select if none set
     */
    'default_country' => 39,

    /**
     * Where should the component place the JavaScript within your view
     */
    'script_stack' => 'post-app-scripts',

    /**
     * Where should the component place the CSS styles within your view
     */
    'style_stack' => 'post-app-css',
];

Publish the package's data (json) files.

php artisan vendor:publish --provider="NerdSnipe\LaravelCountries\LaravelCountriesServiceProvider" --tag="data"

This will copy the countries.json, states.json, and cities.json files to your storage/app/laravel-countries directory.

Example usage

The component relies on providing the id and name of the select element field_name and has the optional values for country, state and city.

This example sets the name attribute of the component to "location", and passes in the IDs of the selected country, state, and city as props, which will pre-select those options when the component is rendered.

The component will then dynamically update the state and city options based on the selected country and state.

Optionally (very useful for the end-user) you can use select2 and set the styles using the props['select2', 'dark'] set either (or both) of them to true to enable the search feature and select2 styling.

<x-laravel-countries::select-location
                field_name="location"
                class="col-4"
                dark=""
                select2=""
                placeholder="{!! __('Select Country') !!}"
                :selected-country="$user->country_id"
                :selected-state="$user->state_id"
                :selected-city="$user->city_id"
    />

Note: Based on the field_name setting (above) access the submitted values as:

$request->location['country_id'] $request->location['state_id'] $request->location['city_id'] 

Use the Facade to get access to the underlying data, remember these are large data-sets

$countriesConnection = LaravelCountries::getCountries(); // Massive Data
$statesCollection = LaravelCountries::getStates($countryID); // Massive Data
$citiesCollection = LaravelCountries::getCities($stateID); // Massive Data

Will return collections as JSON. If you want to use the data you can extract it from the collection

$countryData = $countries->getData();
$stateData = $statesCollection->getData();
$cityData = $citiesCollection->getData();

After using the country, state and city dynamic selector the data will also be available in the cache.

Cache::get("laravel-countries.countries")
Cache::get("laravel-countries.cities.{$selectedState}")
Cache::get("laravel-countries.states.{$selectedCountry}")

TODO

  • Add function to enable a default country, city and state
  • Add ability to focus countries by region as an option
  • Add the use of native in place of name to return country names in their native language
  • Add a gate/middleware to the routes. Because the country select series are likely to be used on registration pages, there is currently no middleware attached to the routes. This exposes the country, state and city json results without any sort of can:view authorization
  • Add a method to cache all states and cities through the Facade (LaravelCountries:cacheAll())

Credits

A special thanks and shout-out to the team at Spatie we appreciate all you do for the Laravel open-source community. You have inspired us to extract useful functionality we have created for internal and client applications and make them available as an open source package. This is the first such package.

License

The Laravel Countries Package is open-sourced software licensed under the MIT license.

Change Log

V1.1

  • Added getCityById, getStateById, getCountryById to the Facade
  • Added ability to hide the labels
  • Added ability to stretch (full-width) the select input

Data available in Countries

[
    {
        "id": 1,
        "name": "Afghanistan",
        "iso3": "AFG",
        "iso2": "AF",
        "numeric_code": "004",
        "phone_code": "93",
        "capital": "Kabul",
        "currency": "AFN",
        "currency_name": "Afghan afghani",
        "currency_symbol": "؋",
        "tld": ".af",
        "native": "افغانستان",
        "region": "Asia",
        "subregion": "Southern Asia",
        "timezones": [
            {
                "zoneName": "Asia\/Kabul",
                "gmtOffset": 16200,
                "gmtOffsetName": "UTC+04:30",
                "abbreviation": "AFT",
                "tzName": "Afghanistan Time"
            }
        ],
        "translations": {
            "kr": "아프가니스탄",
            "pt-BR": "Afeganistão",
            "pt": "Afeganistão",
            "nl": "Afghanistan",
            "hr": "Afganistan",
            "fa": "افغانستان",
            "de": "Afghanistan",
            "es": "Afganistán",
            "fr": "Afghanistan",
            "ja": "アフガニスタン",
            "it": "Afghanistan",
            "cn": "阿富汗",
            "tr": "Afganistan"
        },
        "latitude": "33.00000000",
        "longitude": "65.00000000",
        "emoji": "🇦🇫",
        "emojiU": "U+1F1E6 U+1F1EB"
    }
]

Data Available in States

[
    {
        "id": 3901,
        "name": "Badakhshan",
        "country_id": 1,
        "country_code": "AF",
        "country_name": "Afghanistan",
        "state_code": "BDS",
        "type": null,
        "latitude": "36.73477250",
        "longitude": "70.81199530"
    }
]

Data available in Cities

[
    {
        "id": 52,
        "name": "Ashkāsham",
        "state_id": 3901,
        "state_code": "BDS",
        "state_name": "Badakhshan",
        "country_id": 1,
        "country_code": "AF",
        "country_name": "Afghanistan",
        "latitude": "36.68333000",
        "longitude": "71.53333000",
        "wikiDataId": "Q4805192"
    }
]

nerdsnipe/laravel-countries 适用场景与选型建议

nerdsnipe/laravel-countries 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.09k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-03