Skip to content

Instantly share code, notes, and snippets.

@duongtdvn
Created April 1, 2022 04:12
Show Gist options
  • Select an option

  • Save duongtdvn/7c3ed552db46caefb40035ed582c9aa0 to your computer and use it in GitHub Desktop.

Select an option

Save duongtdvn/7c3ed552db46caefb40035ed582c9aa0 to your computer and use it in GitHub Desktop.
Laravel enum casting for country
<?php
namespace App\Models;
use App\Enums\Country;
use Illuminate\Database\Eloquent\Model;
class Address extends Model
{
protected $casts = [
'country' => Country::class,
];
}
<?php
namespace App\Enums;
enum Country: string
{
case AF = 'AF';
case AX = 'AX';
case AL = 'AL';
case DZ = 'DZ';
case AS = 'AS';
case AD = 'AD';
case AO = 'AO';
case AI = 'AI';
case AQ = 'AQ';
case AG = 'AG';
case AR = 'AR';
case AM = 'AM';
case AW = 'AW';
case AU = 'AU';
case AT = 'AT';
case AZ = 'AZ';
case BS = 'BS';
case BH = 'BH';
case BD = 'BD';
case BB = 'BB';
case BY = 'BY';
case BE = 'BE';
case BZ = 'BZ';
case BJ = 'BJ';
case BM = 'BM';
case BT = 'BT';
case BO = 'BO';
case BQ = 'BQ';
case BA = 'BA';
case BW = 'BW';
case BV = 'BV';
case BR = 'BR';
case IO = 'IO';
case BN = 'BN';
case BG = 'BG';
case BF = 'BF';
case BI = 'BI';
case KH = 'KH';
case CM = 'CM';
case CA = 'CA';
case CV = 'CV';
case KY = 'KY';
case CF = 'CF';
case TD = 'TD';
case CL = 'CL';
case CN = 'CN';
case CX = 'CX';
case CC = 'CC';
case CO = 'CO';
case KM = 'KM';
case CG = 'CG';
case CK = 'CK';
case CR = 'CR';
case CI = 'CI';
case HR = 'HR';
case CU = 'CU';
case CW = 'CW';
case CY = 'CY';
case CZ = 'CZ';
case CD = 'CD';
case DK = 'DK';
case DJ = 'DJ';
case DM = 'DM';
case DO = 'DO';
case TL = 'TL';
case EC = 'EC';
case EG = 'EG';
case SV = 'SV';
case GQ = 'GQ';
case ER = 'ER';
case EE = 'EE';
case ET = 'ET';
case FK = 'FK';
case FO = 'FO';
case FJ = 'FJ';
case FI = 'FI';
case FR = 'FR';
case GF = 'GF';
case PF = 'PF';
case TF = 'TF';
case GA = 'GA';
case GM = 'GM';
case GE = 'GE';
case DE = 'DE';
case GH = 'GH';
case GI = 'GI';
case GR = 'GR';
case GL = 'GL';
case GD = 'GD';
case GP = 'GP';
case GU = 'GU';
case GT = 'GT';
case GG = 'GG';
case GN = 'GN';
case GW = 'GW';
case GY = 'GY';
case HT = 'HT';
case HM = 'HM';
case HN = 'HN';
case HK = 'HK';
case HU = 'HU';
case IS = 'IS';
case IN = 'IN';
case ID = 'ID';
case IR = 'IR';
case IQ = 'IQ';
case IE = 'IE';
case IL = 'IL';
case IT = 'IT';
case JM = 'JM';
case JP = 'JP';
case JE = 'JE';
case JO = 'JO';
case KZ = 'KZ';
case KE = 'KE';
case KI = 'KI';
case XK = 'XK';
case KW = 'KW';
case KG = 'KG';
case LA = 'LA';
case LV = 'LV';
case LB = 'LB';
case LS = 'LS';
case LR = 'LR';
case LY = 'LY';
case LI = 'LI';
case LT = 'LT';
case LU = 'LU';
case MO = 'MO';
case MK = 'MK';
case MG = 'MG';
case MW = 'MW';
case MY = 'MY';
case MV = 'MV';
case ML = 'ML';
case MT = 'MT';
case IM = 'IM';
case MH = 'MH';
case MQ = 'MQ';
case MR = 'MR';
case MU = 'MU';
case YT = 'YT';
case MX = 'MX';
case FM = 'FM';
case MD = 'MD';
case MC = 'MC';
case MN = 'MN';
case ME = 'ME';
case MS = 'MS';
case MA = 'MA';
case MZ = 'MZ';
case MM = 'MM';
case NA = 'NA';
case NR = 'NR';
case NP = 'NP';
case NL = 'NL';
case NC = 'NC';
case NZ = 'NZ';
case NI = 'NI';
case NE = 'NE';
case NG = 'NG';
case NU = 'NU';
case NF = 'NF';
case KP = 'KP';
case MP = 'MP';
case NO = 'NO';
case OM = 'OM';
case PK = 'PK';
case PW = 'PW';
case PS = 'PS';
case PA = 'PA';
case PG = 'PG';
case PY = 'PY';
case PE = 'PE';
case PH = 'PH';
case PN = 'PN';
case PL = 'PL';
case PT = 'PT';
case PR = 'PR';
case QA = 'QA';
case RE = 'RE';
case RO = 'RO';
case RU = 'RU';
case RW = 'RW';
case SH = 'SH';
case KN = 'KN';
case LC = 'LC';
case PM = 'PM';
case VC = 'VC';
case BL = 'BL';
case MF = 'MF';
case WS = 'WS';
case SM = 'SM';
case ST = 'ST';
case SA = 'SA';
case SN = 'SN';
case RS = 'RS';
case SC = 'SC';
case SL = 'SL';
case SG = 'SG';
case SX = 'SX';
case SK = 'SK';
case SI = 'SI';
case SB = 'SB';
case SO = 'SO';
case ZA = 'ZA';
case GS = 'GS';
case KR = 'KR';
case SS = 'SS';
case ES = 'ES';
case LK = 'LK';
case SD = 'SD';
case SR = 'SR';
case SJ = 'SJ';
case SZ = 'SZ';
case SE = 'SE';
case CH = 'CH';
case SY = 'SY';
case TW = 'TW';
case TJ = 'TJ';
case TZ = 'TZ';
case TH = 'TH';
case TG = 'TG';
case TK = 'TK';
case TO = 'TO';
case TT = 'TT';
case TN = 'TN';
case TR = 'TR';
case TM = 'TM';
case TC = 'TC';
case TV = 'TV';
case UG = 'UG';
case UA = 'UA';
case AE = 'AE';
case GB = 'GB';
case US = 'US';
case UM = 'UM';
case UY = 'UY';
case UZ = 'UZ';
case VU = 'VU';
case VA = 'VA';
case VE = 'VE';
case VN = 'VN';
case VG = 'VG';
case VI = 'VI';
case WF = 'WF';
case EH = 'EH';
case YE = 'YE';
case ZM = 'ZM';
case ZW = 'ZW';
public function name()
{
return match ($this) {
self::AF => 'Afghanistan',
self::AX => 'Aland Islands',
self::AL => 'Albania',
self::DZ => 'Algeria',
self::AS => 'American Samoa',
self::AD => 'Andorra',
self::AO => 'Angola',
self::AI => 'Anguilla',
self::AQ => 'Antarctica',
self::AG => 'Antigua And Barbuda',
self::AR => 'Argentina',
self::AM => 'Armenia',
self::AW => 'Aruba',
self::AU => 'Australia',
self::AT => 'Austria',
self::AZ => 'Azerbaijan',
self::BS => 'Bahamas The',
self::BH => 'Bahrain',
self::BD => 'Bangladesh',
self::BB => 'Barbados',
self::BY => 'Belarus',
self::BE => 'Belgium',
self::BZ => 'Belize',
self::BJ => 'Benin',
self::BM => 'Bermuda',
self::BT => 'Bhutan',
self::BO => 'Bolivia',
self::BQ => 'Bonaire, Sint Eustatius and Saba',
self::BA => 'Bosnia and Herzegovina',
self::BW => 'Botswana',
self::BV => 'Bouvet Island',
self::BR => 'Brazil',
self::IO => 'British Indian Ocean Territory',
self::BN => 'Brunei',
self::BG => 'Bulgaria',
self::BF => 'Burkina Faso',
self::BI => 'Burundi',
self::KH => 'Cambodia',
self::CM => 'Cameroon',
self::CA => 'Canada',
self::CV => 'Cape Verde',
self::KY => 'Cayman Islands',
self::CF => 'Central African Republic',
self::TD => 'Chad',
self::CL => 'Chile',
self::CN => 'China',
self::CX => 'Christmas Island',
self::CC => 'Cocos (Keeling) Islands',
self::CO => 'Colombia',
self::KM => 'Comoros',
self::CG => 'Congo',
self::CK => 'Cook Islands',
self::CR => 'Costa Rica',
self::CI => 'Cote D Ivoire (Ivory Coast)',
self::HR => 'Croatia',
self::CU => 'Cuba',
self::CW => 'Curaçao',
self::CY => 'Cyprus',
self::CZ => 'Czech Republic',
self::CD => 'Democratic Republic of the Congo',
self::DK => 'Denmark',
self::DJ => 'Djibouti',
self::DM => 'Dominica',
self::DO => 'Dominican Republic',
self::TL => 'East Timor',
self::EC => 'Ecuador',
self::EG => 'Egypt',
self::SV => 'El Salvador',
self::GQ => 'Equatorial Guinea',
self::ER => 'Eritrea',
self::EE => 'Estonia',
self::ET => 'Ethiopia',
self::FK => 'Falkland Islands',
self::FO => 'Faroe Islands',
self::FJ => 'Fiji Islands',
self::FI => 'Finland',
self::FR => 'France',
self::GF => 'French Guiana',
self::PF => 'French Polynesia',
self::TF => 'French Southern Territories',
self::GA => 'Gabon',
self::GM => 'Gambia The',
self::GE => 'Georgia',
self::DE => 'Germany',
self::GH => 'Ghana',
self::GI => 'Gibraltar',
self::GR => 'Greece',
self::GL => 'Greenland',
self::GD => 'Grenada',
self::GP => 'Guadeloupe',
self::GU => 'Guam',
self::GT => 'Guatemala',
self::GG => 'Guernsey and Alderney',
self::GN => 'Guinea',
self::GW => 'Guinea-Bissau',
self::GY => 'Guyana',
self::HT => 'Haiti',
self::HM => 'Heard Island and McDonald Islands',
self::HN => 'Honduras',
self::HK => 'Hong Kong S.A.R.',
self::HU => 'Hungary',
self::IS => 'Iceland',
self::IN => 'India',
self::ID => 'Indonesia',
self::IR => 'Iran',
self::IQ => 'Iraq',
self::IE => 'Ireland',
self::IL => 'Israel',
self::IT => 'Italy',
self::JM => 'Jamaica',
self::JP => 'Japan',
self::JE => 'Jersey',
self::JO => 'Jordan',
self::KZ => 'Kazakhstan',
self::KE => 'Kenya',
self::KI => 'Kiribati',
self::XK => 'Kosovo',
self::KW => 'Kuwait',
self::KG => 'Kyrgyzstan',
self::LA => 'Laos',
self::LV => 'Latvia',
self::LB => 'Lebanon',
self::LS => 'Lesotho',
self::LR => 'Liberia',
self::LY => 'Libya',
self::LI => 'Liechtenstein',
self::LT => 'Lithuania',
self::LU => 'Luxembourg',
self::MO => 'Macau S.A.R.',
self::MK => 'Macedonia',
self::MG => 'Madagascar',
self::MW => 'Malawi',
self::MY => 'Malaysia',
self::MV => 'Maldives',
self::ML => 'Mali',
self::MT => 'Malta',
self::IM => 'Man (Isle of)',
self::MH => 'Marshall Islands',
self::MQ => 'Martinique',
self::MR => 'Mauritania',
self::MU => 'Mauritius',
self::YT => 'Mayotte',
self::MX => 'Mexico',
self::FM => 'Micronesia',
self::MD => 'Moldova',
self::MC => 'Monaco',
self::MN => 'Mongolia',
self::ME => 'Montenegro',
self::MS => 'Montserrat',
self::MA => 'Morocco',
self::MZ => 'Mozambique',
self::MM => 'Myanmar',
self::NA => 'Namibia',
self::NR => 'Nauru',
self::NP => 'Nepal',
self::NL => 'Netherlands',
self::NC => 'New Caledonia',
self::NZ => 'New Zealand',
self::NI => 'Nicaragua',
self::NE => 'Niger',
self::NG => 'Nigeria',
self::NU => 'Niue',
self::NF => 'Norfolk Island',
self::KP => 'North Korea',
self::MP => 'Northern Mariana Islands',
self::NO => 'Norway',
self::OM => 'Oman',
self::PK => 'Pakistan',
self::PW => 'Palau',
self::PS => 'Palestinian Territory Occupied',
self::PA => 'Panama',
self::PG => 'Papua new Guinea',
self::PY => 'Paraguay',
self::PE => 'Peru',
self::PH => 'Philippines',
self::PN => 'Pitcairn Island',
self::PL => 'Poland',
self::PT => 'Portugal',
self::PR => 'Puerto Rico',
self::QA => 'Qatar',
self::RE => 'Reunion',
self::RO => 'Romania',
self::RU => 'Russia',
self::RW => 'Rwanda',
self::SH => 'Saint Helena',
self::KN => 'Saint Kitts And Nevis',
self::LC => 'Saint Lucia',
self::PM => 'Saint Pierre and Miquelon',
self::VC => 'Saint Vincent And The Grenadines',
self::BL => 'Saint-Barthelemy',
self::MF => 'Saint-Martin (French part)',
self::WS => 'Samoa',
self::SM => 'San Marino',
self::ST => 'Sao Tome and Principe',
self::SA => 'Saudi Arabia',
self::SN => 'Senegal',
self::RS => 'Serbia',
self::SC => 'Seychelles',
self::SL => 'Sierra Leone',
self::SG => 'Singapore',
self::SX => 'Sint Maarten (Dutch part)',
self::SK => 'Slovakia',
self::SI => 'Slovenia',
self::SB => 'Solomon Islands',
self::SO => 'Somalia',
self::ZA => 'South Africa',
self::GS => 'South Georgia',
self::KR => 'South Korea',
self::SS => 'South Sudan',
self::ES => 'Spain',
self::LK => 'Sri Lanka',
self::SD => 'Sudan',
self::SR => 'Suriname',
self::SJ => 'Svalbard And Jan Mayen Islands',
self::SZ => 'Swaziland',
self::SE => 'Sweden',
self::CH => 'Switzerland',
self::SY => 'Syria',
self::TW => 'Taiwan',
self::TJ => 'Tajikistan',
self::TZ => 'Tanzania',
self::TH => 'Thailand',
self::TG => 'Togo',
self::TK => 'Tokelau',
self::TO => 'Tonga',
self::TT => 'Trinidad And Tobago',
self::TN => 'Tunisia',
self::TR => 'Turkey',
self::TM => 'Turkmenistan',
self::TC => 'Turks And Caicos Islands',
self::TV => 'Tuvalu',
self::UG => 'Uganda',
self::UA => 'Ukraine',
self::AE => 'United Arab Emirates',
self::GB => 'United Kingdom',
self::US => 'United States',
self::UM => 'United States Minor Outlying Islands',
self::UY => 'Uruguay',
self::UZ => 'Uzbekistan',
self::VU => 'Vanuatu',
self::VA => 'Vatican City State (Holy See)',
self::VE => 'Venezuela',
self::VN => 'Vietnam',
self::VG => 'Virgin Islands (British)',
self::VI => 'Virgin Islands (US)',
self::WF => 'Wallis And Futuna Islands',
self::EH => 'Western Sahara',
self::YE => 'Yemen',
self::ZM => 'Zambia',
self::ZW => 'Zimbabwe',
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment