|
4 | 4 |
|
5 | 5 | namespace Bkfdev\Addressable\Models; |
6 | 6 |
|
| 7 | +use Bkfdev\World\Models\City; |
| 8 | +use Bkfdev\World\Models\Country; |
| 9 | +use Bkfdev\World\Models\State; |
7 | 10 | use Illuminate\Database\Eloquent\Model; |
8 | 11 | use Illuminate\Database\Eloquent\Builder; |
9 | 12 | use Illuminate\Database\Eloquent\SoftDeletes; |
10 | 13 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
11 | 14 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
12 | 15 |
|
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 | | - */ |
56 | 16 | class Address extends Model |
57 | 17 | { |
58 | 18 | use HasFactory; |
59 | 19 | use SoftDeletes; |
60 | 20 |
|
61 | | - protected $latColumn = 'latitude'; |
| 21 | + protected $with = ['country', 'state', 'city']; |
62 | 22 |
|
63 | | - protected $lngColumn = 'longitude'; |
64 | | - |
65 | | - /** |
66 | | - * {@inheritdoc} |
67 | | - */ |
68 | 23 | protected $fillable = [ |
69 | 24 | 'addressable_id', |
70 | 25 | 'addressable_type', |
71 | 26 | 'label', |
72 | | - 'country_code', |
| 27 | + 'country_id', |
| 28 | + 'state_id', |
| 29 | + 'city_id', |
73 | 30 | 'street', |
74 | | - 'state', |
75 | | - 'city', |
76 | 31 | 'postal_code', |
77 | 32 | 'latitude', |
78 | 33 | 'longitude', |
79 | 34 | 'is_primary', |
| 35 | + 'is_billing', |
| 36 | + 'is_shipping', |
80 | 37 | ]; |
81 | 38 |
|
82 | | - /** |
83 | | - * {@inheritdoc} |
84 | | - */ |
| 39 | + |
85 | 40 | protected $casts = [ |
86 | 41 | 'addressable_id' => 'integer', |
87 | 42 | 'addressable_type' => 'string', |
88 | 43 | 'label' => 'string', |
89 | | - 'country_code' => 'string', |
90 | 44 | 'street' => 'string', |
91 | | - 'state' => 'string', |
92 | | - 'city' => 'string', |
93 | 45 | 'postal_code' => 'string', |
94 | 46 | 'latitude' => 'float', |
95 | 47 | 'longitude' => 'float', |
96 | 48 | 'is_primary' => 'boolean', |
| 49 | + 'is_billing' => 'boolean', |
| 50 | + 'is_shipping' => 'boolean', |
97 | 51 | 'deleted_at' => 'datetime', |
98 | 52 | ]; |
99 | 53 |
|
100 | | - /** |
101 | | - * Create a new Eloquent model instance. |
102 | | - * |
103 | | - * @param array $attributes |
104 | | - */ |
105 | 54 | public function __construct(array $attributes = []) |
106 | 55 | { |
107 | 56 | $this->setTable(config('laravel-address.tables.addresses')); |
108 | 57 |
|
109 | 58 | parent::__construct($attributes); |
110 | 59 | } |
111 | 60 |
|
112 | | - /** |
113 | | - * Get the owner model of the address. |
114 | | - * |
115 | | - * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
116 | | - */ |
117 | 61 | public function addressable(): MorphTo |
118 | 62 | { |
119 | 63 | return $this->morphTo('addressable', 'addressable_type', 'addressable_id', 'id'); |
120 | 64 | } |
121 | 65 |
|
122 | | - /** |
123 | | - * Scope primary addresses. |
124 | | - * |
125 | | - * @param \Illuminate\Database\Eloquent\Builder $builder |
126 | | - * |
127 | | - * @return \Illuminate\Database\Eloquent\Builder |
128 | | - */ |
129 | 66 | public function scopeIsPrimary(Builder $builder): Builder |
130 | 67 | { |
131 | 68 | return $builder->where('is_primary', true); |
132 | 69 | } |
133 | 70 |
|
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() |
143 | 87 | { |
144 | | - return $builder->where('country_code', $countryCode); |
| 88 | + return $this->belongsTo(City::class); |
145 | 89 | } |
146 | 90 | } |
0 commit comments