1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Address; |
4
|
|
|
|
5
|
|
|
use Yajra\Address\Entities\City; |
6
|
|
|
use Yajra\Address\Entities\Region; |
7
|
|
|
use Yajra\Address\Entities\Province; |
8
|
|
|
use Yajra\Address\Entities\Barangay; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property string address |
12
|
|
|
* @property string street |
13
|
|
|
* @property string region_id |
14
|
|
|
* @property Region region |
15
|
|
|
* @property string province_id |
16
|
|
|
* @property Province province |
17
|
|
|
* @property string city_id |
18
|
|
|
* @property City city |
19
|
|
|
* @property string barangay_id |
20
|
|
|
* @property Barangay barangay |
21
|
|
|
*/ |
22
|
|
|
trait HasAddress |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
26
|
|
|
*/ |
27
|
|
|
public function region() |
28
|
|
|
{ |
29
|
|
|
return $this->belongsTo(config('address.model.region', Region::class), 'region_id', 'region_id')->withDefault(); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
34
|
|
|
*/ |
35
|
|
|
public function province() |
36
|
|
|
{ |
37
|
|
|
return $this->belongsTo(config('address.model.province', Province::class), 'province_id', 'province_id')->withDefault(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
42
|
|
|
*/ |
43
|
|
|
public function city() |
44
|
|
|
{ |
45
|
|
|
return $this->belongsTo(config('address.model.city', City::class), 'city_id', 'city_id')->withDefault(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
50
|
|
|
*/ |
51
|
|
|
public function barangay() |
52
|
|
|
{ |
53
|
|
|
return $this->belongsTo(config('address.model.barangay', Barangay::class), 'barangay_id', 'code')->withDefault(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getAddressAttribute() |
60
|
|
|
{ |
61
|
|
|
return sprintf("%s %s, %s, %s", |
62
|
|
|
$this->street, |
63
|
|
|
$this->barangay->name, |
64
|
|
|
$this->city->name, |
65
|
|
|
$this->province->name |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.