|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Signifly\Addresses\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
8
|
|
|
|
|
9
|
|
|
class Address extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The attributes that aren't mass assignable. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $guarded = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The attributes that should be cast to native types. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $casts = [ |
|
24
|
|
|
'is_primary' => 'bool', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(array $attributes = []) |
|
28
|
|
|
{ |
|
29
|
|
|
if (! isset($this->table)) { |
|
30
|
|
|
$this->setTable(config('addresses.table_name')); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
parent::__construct($attributes); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The associated addressable relation. |
|
38
|
|
|
* |
|
39
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
|
40
|
|
|
*/ |
|
41
|
|
|
public function addressable(): MorphTo |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->morphTo(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Scope a query to include the billing address(es). |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
50
|
|
|
* @param bool $value |
|
51
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
52
|
|
|
*/ |
|
53
|
|
|
public function scopeBilling(Builder $query, bool $value = true): Builder |
|
54
|
|
|
{ |
|
55
|
|
|
return $query->where('is_billing', $value); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Scope a query to include the primary address(es). |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
62
|
|
|
* @param bool $value |
|
63
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
64
|
|
|
*/ |
|
65
|
|
|
public function scopePrimary(Builder $query, bool $value = true): Builder |
|
66
|
|
|
{ |
|
67
|
|
|
return $query->where('is_primary', $value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Scope a query to include the shipping address(es). |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
74
|
|
|
* @param bool $value |
|
75
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
76
|
|
|
*/ |
|
77
|
|
|
public function scopeShipping(Builder $query, bool $value = true): Builder |
|
78
|
|
|
{ |
|
79
|
|
|
return $query->where('is_shipping', $value); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|