1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Testing\Models; |
4
|
|
|
|
5
|
|
|
use Database\Factories\PeopleFactory; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
8
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
10
|
|
|
use Sfneal\Address\Models\Address; |
11
|
|
|
use Sfneal\Builders\QueryBuilder; |
12
|
|
|
use Sfneal\Models\Model; |
13
|
|
|
|
14
|
|
|
class People extends Model |
15
|
|
|
{ |
16
|
|
|
use HasFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The attributes that should be mutated to dates. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $dates = ['deleted_at', 'updated_at', 'created_at']; |
24
|
|
|
|
25
|
|
|
protected $table = 'people'; |
26
|
|
|
protected $primaryKey = 'person_id'; |
27
|
|
|
|
28
|
|
|
protected $fillable = [ |
29
|
|
|
'person_id', |
30
|
|
|
'name_first', |
31
|
|
|
'name_last', |
32
|
|
|
'email', |
33
|
|
|
'age', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Model Factory. |
38
|
|
|
* |
39
|
|
|
* @return PeopleFactory |
40
|
|
|
*/ |
41
|
|
|
protected static function newFactory(): PeopleFactory |
42
|
|
|
{ |
43
|
|
|
return new PeopleFactory(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Query Builder. |
48
|
|
|
* |
49
|
|
|
* @param $query |
50
|
|
|
* @return QueryBuilder |
51
|
|
|
*/ |
52
|
|
|
public function newEloquentBuilder($query) |
53
|
|
|
{ |
54
|
|
|
return new QueryBuilder($query); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Query Builder method for improved type hinting. |
59
|
|
|
* |
60
|
|
|
* @return QueryBuilder|Builder |
61
|
|
|
*/ |
62
|
|
|
public static function query() |
63
|
|
|
{ |
64
|
|
|
return parent::query(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* User's address. |
69
|
|
|
* |
70
|
|
|
* @return MorphOne |
71
|
|
|
*/ |
72
|
|
|
public function address() |
73
|
|
|
{ |
74
|
|
|
return $this->morphOne(Address::class, 'addressable'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve the 'name_full' attribute. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getNameFullAttribute(): string |
83
|
|
|
{ |
84
|
|
|
return $this->attributes['name_first'].' '.$this->attributes['name_last']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retrieve the 'name_last_first' attribute. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getNameLastFirstAttribute(): string |
93
|
|
|
{ |
94
|
|
|
return $this->attributes['name_last'].', '.$this->attributes['name_first']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieve the 'age' attribute. |
99
|
|
|
* |
100
|
|
|
* @return Attribute |
101
|
|
|
*/ |
102
|
|
|
public function age(): Attribute |
103
|
|
|
{ |
104
|
|
|
return Attribute::make( |
105
|
|
|
get: fn ($value) => intval($value) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|