~/docs
Resonate
Resonate
2.x 2.2.2, current
Laravel Countries
Toggle sidebar
Laravel Countries
webpatser/laravel-countries 2.2.2

Laravel Countries 2.x

Collection Macros

Filter and extract country data with the Collection and Str macros that Laravel Countries registers.

The service provider registers macros on Illuminate\Support\Collection and Illuminate\Support\Str when the package boots. The filter macros work on arrays and objects. withFlags() only adds display_name to arrays and plain objects with real name and flag properties, not to Eloquent models.

Available Collection Macros

Macro Purpose Returns
byCountry($code) Items whose country_code equals the code (upper-cased) Collection
byRegion($region) Items whose region contains the string, case-insensitive Collection
byCurrency($code) Items whose currency_code equals the code, case-insensitive Collection
searchCountries($query) Items whose name, capital, iso_3166_2 or iso_3166_3 contains the query Collection
withFlags() Adds display_name (flag, space, name) to each item Collection
countryNames() Plucks name Collection
countryCodes() Plucks iso_3166_2 Collection
countryFlags() Plucks flag, dropping empty values Collection
uniqueRegions() Sorted unique region values Collection
uniqueCurrencies() Sorted unique currency_code values Collection

Try it

Select Macro to Test
Region Filter

Results (20 items)

๐Ÿ‡ฆ๐Ÿ‡ฉ

Andorra

AD

Region: europe
Currency: EUR
๐Ÿ‡ฆ๐Ÿ‡ฑ

Albania

AL

Region: europe
Currency: ALL
๐Ÿ‡ฆ๐Ÿ‡น

Austria

AT

Region: europe
Currency: EUR
๐Ÿ‡ฆ๐Ÿ‡ฝ

ร…land Islands

AX

Region: europe
Currency: EUR
๐Ÿ‡ง๐Ÿ‡ฆ

Bosnia and Herzegovina

BA

Region: europe
Currency: BAM
๐Ÿ‡ง๐Ÿ‡ช

Belgium

BE

Region: europe
Currency: EUR
๐Ÿ‡ง๐Ÿ‡ฌ

Bulgaria

BG

Region: europe
Currency: BGN
๐Ÿ‡ง๐Ÿ‡พ

Belarus

BY

Region: europe
Currency: BYN
๐Ÿ‡จ๐Ÿ‡ญ

Switzerland

CH

Region: europe
Currency: CHF
๐Ÿ‡จ๐Ÿ‡พ

Cyprus

CY

Region: europe
Currency: EUR
๐Ÿ‡จ๐Ÿ‡ฟ

Czechia

CZ

Region: europe
Currency: CZK
๐Ÿ‡ฉ๐Ÿ‡ช

Germany

DE

Region: europe
Currency: EUR
๐Ÿ‡ฉ๐Ÿ‡ฐ

Denmark

DK

Region: europe
Currency: DKK
๐Ÿ‡ช๐Ÿ‡ช

Estonia

EE

Region: europe
Currency: EUR
๐Ÿ‡ช๐Ÿ‡ธ

Spain

ES

Region: europe
Currency: EUR
๐Ÿ‡ซ๐Ÿ‡ฎ

Finland

FI

Region: europe
Currency: EUR
๐Ÿ‡ซ๐Ÿ‡ด

Faroe Islands

FO

Region: europe
Currency: DKK
๐Ÿ‡ซ๐Ÿ‡ท

France

FR

Region: europe
Currency: EUR
๐Ÿ‡ฌ๐Ÿ‡ง

United Kingdom

GB

Region: europe
Currency: GBP
๐Ÿ‡ฌ๐Ÿ‡ฌ

Guernsey

GG

Region: europe
Currency: GGP

Generated Code:

$countries->byRegion('Europe')

About Filter by Region

Filters countries by their geographical region. Useful for regional analysis and targeting specific markets.

Basic Usage

Create a collection from the country list:

use Webpatser\Countries\Countries;
 
$countries = collect((new Countries())->getList());

Or use any collection with the same keys:

$someCountries = collect([
'US' => ['name' => 'United States', 'region' => 'americas', 'currency_code' => 'USD'],
'DE' => ['name' => 'Germany', 'region' => 'europe', 'currency_code' => 'EUR'],
'JP' => ['name' => 'Japan', 'region' => 'asia', 'currency_code' => 'JPY'],
]);

Filtering Macros

Filter by Region

$europeanCountries = $countries->byRegion('Europe');
$asianCountries = $countries->byRegion('Asia');
 
$europeanNames = $countries
->byRegion('Europe')
->countryNames()
->sort();

Filter by Currency

$euroCountries = $countries->byCurrency('EUR');
$usdCountries = $countries->byCurrency('USD');
 
$euroCountryNames = $countries
->byCurrency('EUR')
->countryNames();

Search Countries

searchCountries() is case-insensitive. It does not look at full_name; use Countries::search() for that.

$unitedCountries = $countries->searchCountries('united');
$londonResults = $countries->searchCountries('london');
$franceResults = $countries->searchCountries('FRANCE');
$byCode = $countries->searchCountries('nld');

Filter by Country Code

byCountry() works on collections whose items carry a country_code field, such as your own users or orders:

$americanUsers = collect($users)->byCountry('US');

Data Extraction Macros

countryNames(), countryCodes() and countryFlags() return a list without the ISO keys. countryFlags() keeps the numeric positions of non-empty values. Use ->pluck('name', 'iso_3166_2') when you need the codes as keys.

$names = $countries->countryNames(); // [0 => 'Andorra', 1 => 'United Arab Emirates', ...]
$codes = $countries->countryCodes(); // [0 => 'AD', 1 => 'AE', ...]
$flags = $countries->countryFlags(); // [0 => '๐Ÿ‡ฆ๐Ÿ‡ฉ', 1 => '๐Ÿ‡ฆ๐Ÿ‡ช', ...]
 
$regions = $countries->uniqueRegions(); // africa, americas, asia, europe, oceania
$currencies = $countries->uniqueCurrencies();
 
$withDisplayName = $countries->withFlags();
// each item now has 'display_name' => '๐Ÿ‡ฆ๐Ÿ‡ฉ Andorra'

String Macros

use Illuminate\Support\Str;
 
Str::toCountryFlag('US'); // "๐Ÿ‡บ๐Ÿ‡ธ"
Str::toCountryFlag('DE'); // "๐Ÿ‡ฉ๐Ÿ‡ช"
 
Str::fromCountryFlag('๐Ÿ‡บ๐Ÿ‡ธ'); // "US"
Str::fromCountryFlag('๐Ÿ‡ฉ๐Ÿ‡ช'); // "DE"
 
Str::countryName('FR'); // "France"
Str::countryName('JP'); // "Japan"

toCountryFlag() returns an empty string when the input is not exactly two characters; it does not check that they are letters. fromCountryFlag() returns an empty string for anything that is not two regional-indicator symbols. countryName() returns an empty string for an unknown code.

Advanced Chaining

use Illuminate\Support\Str;
 
// European country names, sorted alphabetically
$result = $countries
->byRegion('Europe')
->countryNames()
->sort()
->values();
 
// Currencies used in Asia
$asianCurrencies = $countries
->byRegion('Asia')
->pluck('currency_code')
->unique()
->sort()
->values();
 
// Flags of countries with "United" in the name
$unitedFlags = $countries
->searchCountries('United')
->countryFlags();
 
// European countries using the Euro, as a labelled list
$euroEuropean = $countries
->byRegion('Europe')
->byCurrency('EUR')
->map(fn ($country, $code) => Str::toCountryFlag($code) . ' ' . $country['name'])
->sort()
->values();

Real-world Use Cases

Building Select Options

$europeanOptions = $countries
->byRegion('Europe')
->map(fn ($country, $code) => [
'value' => $code,
'label' => $country['flag'] . ' ' . $country['name'],
])
->sortBy('label')
->values();

Analytics and Reporting

$currencyGroups = $countries
->groupBy('currency_code')
->map(fn ($group) => $group->countryNames()->sort())
->sortKeys();
 
$regionStats = $countries
->groupBy('region')
->map(fn ($group) => [
'count' => $group->count(),
'currencies' => $group->pluck('currency_code')->unique()->count(),
'countries' => $group->countryNames()->sort()->take(5)->values(),
]);

API Response Formatting

$apiResponse = $countries
->when($request->region, fn ($c) => $c->byRegion($request->region))
->when($request->currency, fn ($c) => $c->byCurrency($request->currency))
->when($request->search, fn ($c) => $c->searchCountries($request->search))
->take(20)
->map(fn ($country, $code) => [
'code' => $code,
'name' => $country['name'],
'flag' => $country['flag'],
'currency' => [
'code' => $country['currency_code'],
'symbol' => $country['currency_symbol'],
],
])
->values();

Note: The macros filter in PHP on the collection you give them. They do not run database queries. For database filtering use the Eloquent scopes.

Next Steps