1 | <?php |
||
2 | |||
3 | namespace Turahe\Master\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
6 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
7 | use Illuminate\Support\Facades\Cache; |
||
8 | |||
9 | /** |
||
10 | * Class District. |
||
11 | * |
||
12 | * @property string city |
||
13 | * @property string name |
||
14 | * @mixin \Eloquent |
||
15 | */ |
||
16 | class District extends Model |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $table = 'tm_districts'; |
||
22 | |||
23 | /** |
||
24 | * @var string[] |
||
25 | */ |
||
26 | protected $casts = [ |
||
27 | 'meta' => 'array', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * Bootstrap the model and its traits. |
||
32 | * |
||
33 | * Caching model when updating and |
||
34 | * delete cache when delete models |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | protected static function boot() |
||
39 | { |
||
40 | parent::boot(); // TODO: Change the autogenerated stub |
||
41 | static::updating(function ($instance) { |
||
42 | Cache::put('districts.'.$instance->slug, $instance); |
||
43 | }); |
||
44 | static::deleting(function ($instance) { |
||
45 | Cache::delete('districts.'.$instance->slug); |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return BelongsTo |
||
51 | */ |
||
52 | public function city(): BelongsTo |
||
53 | { |
||
54 | return $this->belongsTo(City::class, 'city_id'); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return HasMany |
||
59 | */ |
||
60 | public function villages(): HasMany |
||
61 | { |
||
62 | return $this->hasMany(Village::class, 'district_id'); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function getCityNameAttribute() |
||
69 | { |
||
70 | return $this->city->name; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function getProvinceNameAttribute() |
||
77 | { |
||
78 | return $this->city->province->name; |
||
0 ignored issues
–
show
|
|||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getAddressAttribute(): string |
||
85 | { |
||
86 | return sprintf( |
||
87 | '%s, %s, %s, Indonesia', |
||
88 | $this->name, |
||
89 | $this->city->name, |
||
90 | $this->city->province->name |
||
0 ignored issues
–
show
|
|||
91 | ); |
||
92 | } |
||
93 | } |
||
94 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.