Skip to content

Commit ac79321

Browse files
Add country code attribute
1 parent 4623dbb commit ac79321

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Traits/HasCountry.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @package Lecturize\Addresses\Traits;
1111
* @property int|null $country_id
1212
* @property Countries|null $country
13+
* @property string $country_code
1314
*/
1415
trait HasCountry
1516
{
@@ -23,6 +24,19 @@ public function country(): BelongsTo
2324
return $this->belongsTo(Countries::class);
2425
}
2526

27+
/**
28+
* Get the models country code.
29+
*
30+
* @return string
31+
*/
32+
public function getCountryCodeAttribute(): string
33+
{
34+
if ($country = $this->country)
35+
return $country->iso_3166_2;
36+
37+
return '';
38+
}
39+
2640
/**
2741
* Scope by country.
2842
*

0 commit comments

Comments
 (0)