PHPackages                             nerdsnipe/laravel-countries - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. nerdsnipe/laravel-countries

ActiveLibrary[Templating &amp; Views](/categories/templating)

nerdsnipe/laravel-countries
===========================

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

v1.2.0(3y ago)44.9k↑16.7%2MITPHPPHP ^8.1

Since Apr 5Pushed 2y ago1 watchersCompare

[ Source](https://github.com/NerdSnipe/laravel-countries)[ Packagist](https://packagist.org/packages/nerdsnipe/laravel-countries)[ Docs](https://github.com/nerdsnipe/laravel-countries)[ RSS](/packages/nerdsnipe-laravel-countries/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (3)Used By (0)

Laravel Countries Package
=========================

[](#laravel-countries-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/27f409456a60762dc94f84bde9608bf3f2f396c76ad2251e7599f8a06dae5412/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e657264736e6970652f6c61726176656c2d636f756e74726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nerdsnipe/laravel-countries)[![Total Downloads](https://camo.githubusercontent.com/698082c82fe10e22121e7d16fe3d60dbcd45579a00cb73f0822b097703ac5383/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e657264736e6970652f6c61726176656c2d636f756e74726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nerdsnipe/laravel-countries)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 9.0

Installation
------------

[](#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
-------------

[](#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.

```

```

**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
----

[](#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
-------

[](#credits)

- [Countries, States, Cities Database](https://github.com/dr5hn/countries-states-cities-database)
- [Laravel Package Tools](https://github.com/spatie/laravel-package-tools)

A special thanks and shout-out to the team at [Spatie](https://github.com/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
-------

[](#license)

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

Change Log
----------

[](#change-log)

### V1.1

[](#v11)

- 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

[](#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

[](#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

[](#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"
    }
]
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

2

Last Release

1123d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/120449575?v=4)[Robert (Nerd)](/maintainers/NerdSnipe)[@NerdSnipe](https://github.com/NerdSnipe)

---

Top Contributors

[![NerdSnipe](https://avatars.githubusercontent.com/u/120449575?v=4)](https://github.com/NerdSnipe "NerdSnipe (10 commits)")

---

Tags

laravelLaravel-CountriesNerdSnipelaravel country datalaravel state datalaravel city datacountry state city dynamic dependent dropdowncountry state city dropdown

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/nerdsnipe-laravel-countries/health.svg)

```
[![Health](https://phpackages.com/badges/nerdsnipe-laravel-countries/health.svg)](https://phpackages.com/packages/nerdsnipe-laravel-countries)
```

###  Alternatives

[ryangjchandler/blade-capture-directive

Create inline partials in your Blade templates with ease.

8222.2M12](/packages/ryangjchandler-blade-capture-directive)[cagilo/cagilo

A set of open-source Blade components for the Laravel Framework

172996.5k](/packages/cagilo-cagilo)[spatie/laravel-blade-comments

Add debug comments to your rendered output

177325.5k](/packages/spatie-laravel-blade-comments)[daikazu/laravel-glider

Start using Glide on-the-fly instantly in your Laravel blade templates.

882.3k](/packages/daikazu-laravel-glider)[combindma/dash-ui

A streamlined and stylish UI component library for Laravel Blade, crafted with TailwindCSS and AlpineJs for simplicity and elegance.

631.4k](/packages/combindma-dash-ui)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
