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
Results (20 items)
Andorra
AD
Albania
AL
Austria
AT
ร land Islands
AX
Bosnia and Herzegovina
BA
Belgium
BE
Bulgaria
BG
Belarus
BY
Switzerland
CH
Cyprus
CY
Czechia
CZ
Germany
DE
Denmark
DK
Estonia
EE
Spain
ES
Finland
FI
Faroe Islands
FO
France
FR
United Kingdom
GB
Guernsey
GG
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
- Helper Functions: Global helper functions for country data.
- API Reference: Every class, method and function.
- Quick Start: Review the basics and common patterns.