Skip to content

List of Countries

How to get a simple list of countries?

With the getList() helper, you can fetch optimized country lists, perfect for populating dropdowns or similar elements.

If you want a list that includes both the country names and slugs, use the following code

php
use Lwwcas\LaravelCountries\Models\Country;

Country::getList()
    ->withNamesAndSlugs()
    ->get();

If you want a list that includes both the country names, slugs and flags, use the following code

php
use Lwwcas\LaravelCountries\Models\Country;

Country::getList()
    ->withNamesSlugsAndFlags()
    ->get();

Names And Slugs

Output for ->withNamesAndSlugs() on Array version

INFO

You will need to convert the collection to an array.

You can achieve this by using Eloquent's ->toArray() method.

php

Country::getList()->withNamesAndSlugs()->get()->toArray();
php
array:3 [
  0 => array:8 [
    "id" => 3
    "uid" => "01J95Z2ZE5KYX1QJEXYVER62D1"
    "official_name" => "Islamic Emirate of Afghanistan"
    "iso_alpha_2" => "AF"
    "iso_alpha_3" => "AFG"
    "name" => "Afghanistan"
    "slug" => "afghanistan"
    "translations" => array:1 [
      0 => array:5 [
        "id" => 3
        "lc_country_id" => 3
        "name" => "Afghanistan"
        "slug" => "afghanistan"
        "locale" => "en"
      ]
    ]
  ]
  1 => array:8 []
  2 => array:8 []
  ...
]

Names Slugs And Flags

Output for ->withNamesSlugsAndFlags() on Array version

INFO

You will need to convert the collection to an array.

You can achieve this by using Eloquent's ->toArray() method.

php

Country::getList()->withNamesSlugsAndFlags()->get()->toArray();
php
array:3 [
  0 => array:9 [
    "id" => 3
    "uid" => "01J95Z2ZE5KYX1QJEXYVER62D1"
    "official_name" => "Islamic Emirate of Afghanistan"
    "iso_alpha_2" => "AF"
    "iso_alpha_3" => "AFG"
    "flag_emoji" => array:9 [
      "css" => "\1F1E6\1F1EB"
      "hex" => "🇦🇫"
      "img" => "🇦🇫"
      "html" => "🇦🇫"
      "utf8" => "🇦🇫"
      "uCode" => "U+1F1E6 U+1F1EB"
      "utf16" => "\uD83C\uDDE6\uD83C\uDDEB"
      "decimal" => "🇦🇫"
      "shortcode" => ":flag-af:"
    ] 
    "name" => "Afghanistan"
    "slug" => "afghanistan"
    "translations" => array:1 [
      0 => array:5 [
        "id" => 3
        "lc_country_id" => 3
        "name" => "Afghanistan"
        "slug" => "afghanistan"
        "locale" => "en"
      ] 
    ] 
  ]
  1 => array:9 []
  2 => array:9 []
  ...
]

Released under the MIT License.