1010use Illuminate \Database \Eloquent \Model ;
1111use Illuminate \Database \Eloquent \Builder ;
1212use Illuminate \Database \Eloquent \SoftDeletes ;
13- use GeneaLabs \LaravelModelCaching \Traits \Cachable ;
1413use Illuminate \Database \Eloquent \Relations \MorphTo ;
1514use Illuminate \Database \Eloquent \Factories \HasFactory ;
1615
1716class Address extends Model
1817{
1918 use HasFactory;
2019 use SoftDeletes;
21- use Cachable;
22-
23- protected $ with = ['country ' , 'state ' , 'city ' ];
24-
25- protected $ fillable = [
26- 'addressable_id ' ,
27- 'addressable_type ' ,
28- 'label ' ,
29- 'country_id ' ,
30- 'state_id ' ,
31- 'city_id ' ,
32- 'street ' ,
33- 'postal_code ' ,
34- 'latitude ' ,
35- 'longitude ' ,
36- 'is_primary ' ,
37- 'is_billing ' ,
38- 'is_shipping ' ,
39- ];
20+
21+ //protected $with = ['country', 'state', 'city'];
22+
23+ protected $ guarded = [];
4024
4125
4226 protected $ casts = [
43- 'addressable_id ' => 'integer ' ,
44- 'addressable_type ' => 'string ' ,
45- 'label ' => 'string ' ,
46- 'street ' => 'string ' ,
47- 'postal_code ' => 'string ' ,
48- 'latitude ' => 'float ' ,
49- 'longitude ' => 'float ' ,
5027 'is_primary ' => 'boolean ' ,
5128 'is_billing ' => 'boolean ' ,
5229 'is_shipping ' => 'boolean ' ,
53- 'deleted_at ' => 'datetime ' ,
5430 ];
5531
56- public function __construct (array $ attributes = [])
57- {
58- $ this ->setTable (config ('laravel-address.tables.addresses ' ));
59-
60- parent ::__construct ($ attributes );
61- }
62-
6332 public function addressable (): MorphTo
6433 {
6534 return $ this ->morphTo ('addressable ' , 'addressable_type ' , 'addressable_id ' , 'id ' );
@@ -70,6 +39,18 @@ public function scopeIsPrimary(Builder $builder): Builder
7039 return $ builder ->where ('is_primary ' , true );
7140 }
7241
42+ public function scopeIsBilling (Builder $ builder ): Builder
43+ {
44+ return $ builder ->where ('is_billing ' , true );
45+ }
46+ public function scopeIsShipping (Builder $ builder ): Builder
47+ {
48+ return $ builder ->where ('is_shipping ' , true );
49+ }
50+ public function getFullNameAttribute (): string
51+ {
52+ return implode (' ' , [$ this ->given_name , $ this ->family_name ]);
53+ }
7354 public function scopeInCountry (Builder $ builder , string $ countryId ): Builder
7455 {
7556 return $ builder ->where ('country_id ' , $ countryId );
@@ -89,4 +70,31 @@ public function city()
8970 {
9071 return $ this ->belongsTo (City::class);
9172 }
92- }
73+
74+ protected static function boot ()
75+ {
76+ parent ::boot ();
77+
78+ static ::saving (function (self $ address ) {
79+ $ geocoding = config ('laravel-address.geocoding.enabled ' );
80+ $ geocoding_api_key = config ('laravel-address.geocoding.api_key ' );
81+ if ($ geocoding && $ geocoding_api_key ) {
82+ $ segments [] = $ address ->street ;
83+ $ segments [] = sprintf ('%s, %s %s ' , $ address ->city ?->name, $ address ->state ?->name, $ address ->postal_code );
84+ $ segments [] = country ($ address ->country ?->country_code)->getName ();
85+
86+ $ query = str_replace (' ' , '+ ' , implode (', ' , $ segments ));
87+ $ geocode = json_decode (
88+ file_get_contents (
89+ "https://maps.google.com/maps/api/geocode/json?address= {$ query }&sensor=false&key= {$ geocoding_api_key }"
90+ )
91+ );
92+
93+ if (count ($ geocode ->results )) {
94+ $ address ->latitude = $ geocode ->results [0 ]->geometry ->location ->lat ;
95+ $ address ->longitude = $ geocode ->results [0 ]->geometry ->location ->lng ;
96+ }
97+ }
98+ });
99+ }
100+ }
0 commit comments