Skip to content

Commit 989bc8e

Browse files
committed
refactoring
1 parent 99f9401 commit 989bc8e

File tree

14 files changed

+60
-767
lines changed

14 files changed

+60
-767
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

composer.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@
1212
"homepage": "https://github.com/bkfdev/laravel-address",
1313
"keywords": ["Laravel", "LaravelAddress"],
1414
"require": {
15-
"illuminate/support": "~9",
16-
"bkfdev/laravel-world": "*",
17-
"genealabs/laravel-model-caching": "^0.12.4"
18-
},
19-
"require-dev": {
20-
"phpunit/phpunit": "~9.0",
21-
"orchestra/testbench": "~7"
15+
"bkfdev/laravel-world": "*"
2216
},
17+
2318
"autoload": {
2419
"psr-4": {
2520
"Bkfdev\\Addressable\\": "src/"

config/laravel-address.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
'address' => \Bkfdev\Addressable\Models\Address::class,
1212
],
1313

14-
];
14+
// Addresses Geocoding Options
15+
'geocoding' => [
16+
'enabled' => false,
17+
'api_key' => env('GOOGLE_APP_KEY'),
18+
],
19+
];

database/migrations/2020_01_01_000001_create_addresses_table.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ public function up()
1313
Schema::create(config('laravel-address.tables.addresses'), function (Blueprint $table) {
1414
// Columns
1515
$table->increments('id');
16+
$table->string('given_name');
17+
$table->string('family_name')->nullable;
1618
$table->morphs('addressable');
1719
$table->string('label')->nullable();
18-
$table->foreignId('country_id');
19-
$table->foreignId('state_id');
20+
$table->string('organization')->nullable();
21+
$table->foreignId('country_id')->on('countries');
22+
$table->foreignId('state_id')->on('states');
2023
$table->foreignId('city_id');
2124
$table->string('street')->nullable();
2225
$table->string('postal_code')->nullable();
@@ -34,4 +37,4 @@ public function down()
3437
{
3538
Schema::dropIfExists(config('laravel-address.tables.addresses'));
3639
}
37-
}
40+
}

src/Models/Address.php

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,25 @@
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Database\Eloquent\Builder;
1212
use Illuminate\Database\Eloquent\SoftDeletes;
13-
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
1413
use Illuminate\Database\Eloquent\Relations\MorphTo;
1514
use Illuminate\Database\Eloquent\Factories\HasFactory;
1615

1716
class 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+
}

vendor/autoload.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)