Skip to content

Commit 27b8d9f

Browse files
committed
Major update, this package requires bkfdev/laravel-world
1 parent 96cac9c commit 27b8d9f

File tree

3 files changed

+36
-89
lines changed

3 files changed

+36
-89
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"homepage": "https://github.com/bkfdev/laravel-address",
1313
"keywords": ["Laravel", "LaravelAddress"],
1414
"require": {
15-
"illuminate/support": "~9"
15+
"illuminate/support": "~9",
16+
"bkfdev/laravel-world": "@dev"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "~9.0",

database/migrations/2020_01_01_000001_create_addresses_table.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ public function up()
1515
$table->increments('id');
1616
$table->morphs('addressable');
1717
$table->string('label')->nullable();
18-
$table->string('country_code', 2)->nullable();
18+
$table->foreignId('country_id');
19+
$table->foreignId('state_id');
20+
$table->foreignId('city_id');
1921
$table->string('street')->nullable();
20-
$table->string('state')->nullable();
21-
$table->string('city')->nullable();
2222
$table->string('postal_code')->nullable();
2323
$table->decimal('latitude', 10, 7)->nullable();
2424
$table->decimal('longitude', 10, 7)->nullable();
2525
$table->boolean('is_primary')->default(false);
26+
$table->boolean('is_billing')->default(false);
27+
$table->boolean('is_shipping')->default(false);
2628
$table->timestamps();
2729
$table->softDeletes();
2830
});

src/Models/Address.php

Lines changed: 29 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,143 +4,87 @@
44

55
namespace Bkfdev\Addressable\Models;
66

7+
use Bkfdev\World\Models\City;
8+
use Bkfdev\World\Models\Country;
9+
use Bkfdev\World\Models\State;
710
use Illuminate\Database\Eloquent\Model;
811
use Illuminate\Database\Eloquent\Builder;
912
use Illuminate\Database\Eloquent\SoftDeletes;
1013
use Illuminate\Database\Eloquent\Relations\MorphTo;
1114
use Illuminate\Database\Eloquent\Factories\HasFactory;
1215

13-
/**
14-
* Bkfdev\Addressable\Models\Address.
15-
*
16-
* @property int $id
17-
* @property int $addressable_id
18-
* @property string $addressable_type
19-
* @property string $label
20-
* @property string $country_code
21-
* @property string $street
22-
* @property string $state
23-
* @property string $city
24-
* @property string $postal_code
25-
* @property float $latitude
26-
* @property float $longitude
27-
* @property bool $is_primary
28-
* @property bool $is_billing
29-
* @property bool $is_shipping
30-
* @property \Carbon\Carbon|null $created_at
31-
* @property \Carbon\Carbon|null $updated_at
32-
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $addressable
33-
*
34-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address inCountry($countryCode)
35-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address isBilling()
36-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address isPrimary()
37-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address isShipping()
38-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereAddressableId($value)
39-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereAddressableType($value)
40-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereCity($value)
41-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereCountryCode($value)
42-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereCreatedAt($value)
43-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereId($value)
44-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereIsBilling($value)
45-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereIsPrimary($value)
46-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereIsShipping($value)
47-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereLabel($value)
48-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereLatitude($value)
49-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereLongitude($value)
50-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address wherePostalCode($value)
51-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereState($value)
52-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereStreet($value)
53-
* @method static \Illuminate\Database\Eloquent\Builder|\Bkfdev\Addressable\Models\Address whereUpdatedAt($value)
54-
* @mixin \Eloquent
55-
*/
5616
class Address extends Model
5717
{
5818
use HasFactory;
5919
use SoftDeletes;
6020

61-
protected $latColumn = 'latitude';
21+
protected $with = ['country', 'state', 'city'];
6222

63-
protected $lngColumn = 'longitude';
64-
65-
/**
66-
* {@inheritdoc}
67-
*/
6823
protected $fillable = [
6924
'addressable_id',
7025
'addressable_type',
7126
'label',
72-
'country_code',
27+
'country_id',
28+
'state_id',
29+
'city_id',
7330
'street',
74-
'state',
75-
'city',
7631
'postal_code',
7732
'latitude',
7833
'longitude',
7934
'is_primary',
35+
'is_billing',
36+
'is_shipping',
8037
];
8138

82-
/**
83-
* {@inheritdoc}
84-
*/
39+
8540
protected $casts = [
8641
'addressable_id' => 'integer',
8742
'addressable_type' => 'string',
8843
'label' => 'string',
89-
'country_code' => 'string',
9044
'street' => 'string',
91-
'state' => 'string',
92-
'city' => 'string',
9345
'postal_code' => 'string',
9446
'latitude' => 'float',
9547
'longitude' => 'float',
9648
'is_primary' => 'boolean',
49+
'is_billing' => 'boolean',
50+
'is_shipping' => 'boolean',
9751
'deleted_at' => 'datetime',
9852
];
9953

100-
/**
101-
* Create a new Eloquent model instance.
102-
*
103-
* @param array $attributes
104-
*/
10554
public function __construct(array $attributes = [])
10655
{
10756
$this->setTable(config('laravel-address.tables.addresses'));
10857

10958
parent::__construct($attributes);
11059
}
11160

112-
/**
113-
* Get the owner model of the address.
114-
*
115-
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
116-
*/
11761
public function addressable(): MorphTo
11862
{
11963
return $this->morphTo('addressable', 'addressable_type', 'addressable_id', 'id');
12064
}
12165

122-
/**
123-
* Scope primary addresses.
124-
*
125-
* @param \Illuminate\Database\Eloquent\Builder $builder
126-
*
127-
* @return \Illuminate\Database\Eloquent\Builder
128-
*/
12966
public function scopeIsPrimary(Builder $builder): Builder
13067
{
13168
return $builder->where('is_primary', true);
13269
}
13370

134-
/**
135-
* Scope addresses by the given country.
136-
*
137-
* @param \Illuminate\Database\Eloquent\Builder $builder
138-
* @param string $countryCode
139-
*
140-
* @return \Illuminate\Database\Eloquent\Builder
141-
*/
142-
public function scopeInCountry(Builder $builder, string $countryCode): Builder
71+
public function scopeInCountry(Builder $builder, string $countryId): Builder
72+
{
73+
return $builder->where('country_id', $countryId);
74+
}
75+
76+
public function country()
77+
{
78+
return $this->belongsTo(Country::class);
79+
}
80+
81+
public function state()
82+
{
83+
return $this->belongsTo(State::class);
84+
}
85+
86+
public function city()
14387
{
144-
return $builder->where('country_code', $countryCode);
88+
return $this->belongsTo(City::class);
14589
}
14690
}

0 commit comments

Comments
 (0)