nerdsnipe/laravel-countries

用于向任何Laravel视图添加国家、州和城市选择器。提供251个国家的所有城市和州

v1.2.0 2023-04-12 10:07 UTC

This package is auto-updated.

Last update: 2024-09-28 06:14:07 UTC


README

Latest Version on Packagist Total Downloads MIT Licensed

A Laravel包,用于处理国家、州、省和城市。使用此包可以轻松地将动态的国家、州和城市选择元素系列添加到您的Laravel应用程序中。所有必需的数据都包含在包含的JSON文件中,这些文件导入到您的数据库中。

要求

  • PHP >= 8.1
  • Laravel >= 9.0

安装

您可以通过composer安装此包

composer require nerdsnipe/laravel-countries

必须发布包的配置文件。

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

这将发布config/laravel-countries.php文件。

设置script_stackstyle_stack以反映在视图或布局中放置脚本和CSS元素的位置。

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',
];

发布包的数据(JSON)文件。

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

这将复制countries.json、states.json和cities.json文件到您的storage/app/laravel-countries目录。

示例用法

组件依赖于提供选择元素的id和name(字段名)field_name,并可选地提供国家、州和城市的值。

此示例将组件的name属性设置为"location",并通过props传入选中国家、州和城市的ID,当组件渲染时将预选中这些选项。

组件将根据选定的国家和州动态更新州和城市选项。

可选地(对最终用户非常有用)您可以使用select2并使用props['select2', 'dark']设置样式,将它们(或两者)设置为true以启用搜索功能和select2样式。

<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"
    />

注意:根据上述field_name设置访问提交的值

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

使用外观获取底层数据,记住这些是大型数据集

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

将返回JSON格式的集合。如果您想使用数据,可以从集合中提取它

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

使用国家、州和城市动态选择器后,数据也将可用于缓存。

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

TODO

  • 添加功能以启用默认国家、城市和州
  • 添加通过region聚焦国家的功能作为选项
  • 添加使用native代替name来返回国家名称的本地语言的功能
  • 将网关/中间件添加到路由中。由于国家选择系列可能用于注册页面,因此目前没有中间件附加到路由上。这会在没有任何can:view授权的情况下公开国家、州和城市JSON结果
  • 添加一个方法通过外观缓存所有州和城市(LaravelCountries:cacheAll()

致谢

特别感谢并致谢Spatie团队,我们感谢您为Laravel开源社区所做的一切。您激励我们将为内部和客户应用程序创建的有用功能提取出来,并将其作为开源包提供。这是第一个这样的包。

许可证

Laravel Countries包是开源软件,许可协议为MIT。

变更日志

V1.1

  • 向外观层添加了 getCityByIdgetStateByIdgetCountryById 功能。
  • 添加了隐藏标签的功能。
  • 添加了扩展(全宽度)选择输入框的功能。

国家数据可用。

[
    {
        "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"
    }
]

州数据可用。

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

城市数据可用。

[
    {
        "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"
    }
]