marshmallow / dataset-country
This dataset contains all the countries in the world. They are translateable in different languages. It is also possible to get the flag from said countries. The model is extendable so you can extend and overide all functionality if needed.
Package info
github.com/marshmallow-packages/dataset-country
pkg:composer/marshmallow/dataset-country
Requires
- php: ^7.4|^8.0
- marshmallow/commands: ^1.0
- marshmallow/sluggable: ^1.0
Suggests
- marshmallow/dataset-google-geotargets: Provices google data on provinces, cities, airports etc.
README
Marshmallow Dataset Country
This dataset contains all the countries in the world. They are translateable in different languages. It is also possible to get the flag from said countries. The model is extendable so you can extend and override all functionality if needed.
Installation
Install the package via Composer:
composer require marshmallow/dataset-country
The service provider is auto-discovered and the package migrations are loaded automatically, so you only need to run your migrations:
php artisan migrate
This creates a countries table with name, slug, alpha2 and alpha3 columns (each unique).
Seed the table
Run the bundled seeder to fill the countries table:
php artisan db:seed --class="Marshmallow\Datasets\Country\Seeders\CountrySeeder"
Publish the flags
To use the flag images you need to publish them to your local project:
php artisan vendor:publish --provider="Marshmallow\Datasets\Country\ServiceProvider" --tag=public --force
You can optionally publish the translations as well:
php artisan vendor:publish --provider="Marshmallow\Datasets\Country\ServiceProvider" --tag=translations
Usage
The package ships an Eloquent model at Marshmallow\Datasets\Country\Models\Country.
use Marshmallow\Datasets\Country\Models\Country; // Look up a country by its ISO alpha-2 code. $country = Country::alpha2('nl'); // Get all countries ordered by name. $countries = Country::ordered()->get();
Methods
- Get the locale (translated) version of a country name with
$country->getNameLocale().
Flags
You can get the flag of a country with $country->flag. If you wish to specify the size of the flag image, you can use $country->flag(16). The available sizes are 16, 24, 32, 48, 64 and 128 (the default is 32). Remember to publish the flags first (see Installation).
Traits
Add the Marshmallow\Datasets\Country\Traits\HasCountry trait to your models that have a country. This sets up the country() relationship:
use Marshmallow\Datasets\Country\Traits\HasCountry; class Address extends Model { use HasCountry; } // $address->country instanceof Country
Nova
Are you using Nova? We have a command for you to generate the Nova Resource. Run php artisan marshmallow:resource Country Datasets\\Country and countries will be available to you in Nova. We hide this resource by default in the Nova navigation. If you wish to have it available in the navigation, add public static $displayInNavigation = true; to app/Nova/Country.php.
Provinces, cities and airports
The model also exposes a provinces() relationship. This requires the optional marshmallow/dataset-google-geotargets package, which provides Google data on provinces, cities, airports etc.
composer require marshmallow/dataset-google-geotargets
Translations
The country names are translated into the following languages:
- Arabic (ar)
- Chinese (cn)
- Czech (cs)
- Danish (da)
- German (de)
- Greek (el)
- English (en)
- Spanish (es)
- Estonian (et)
- French (fr)
- Hungarian (hu)
- Italian (it)
- Japanese (ja)
- Lithuanian (lt)
- Dutch (nl)
- Norwegian (no)
- Polish (pl)
- Portuguese (pt)
- Romanian (ro)
- Russian (ru)
- Slovak (sk)
- Thai (th)
- Ukrainian (uk)
Security Vulnerabilities
Please report security vulnerabilities by email to stef@marshmallow.dev rather than via the public issue tracker.
Credits
License
The MIT License (MIT). Please see the License File for more information.
Copyright (c) 2020 marshmallow