1 | <?php |
||||
2 | |||||
3 | namespace App\Models; |
||||
4 | |||||
5 | //use App\Traits\ConnectionTrait; |
||||
6 | use App\Traits\TenantConnectionResolver; |
||||
7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
8 | use Illuminate\Support\Facades\Auth; |
||||
9 | use Laravel\Scout\Searchable; |
||||
10 | use LaravelEnso\People\Models\Person as CorePerson; |
||||
11 | |||||
12 | class Person extends CorePerson |
||||
13 | { |
||||
14 | use HasFactory; |
||||
15 | use TenantConnectionResolver; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
16 | use Searchable; |
||||
0 ignored issues
–
show
|
|||||
17 | |||||
18 | // public function searchableAs() |
||||
19 | // { |
||||
20 | // return 'name'; |
||||
21 | // } |
||||
22 | |||||
23 | /** |
||||
24 | * The attributes that should be mutated to dates. |
||||
25 | * |
||||
26 | * @var array |
||||
27 | */ |
||||
28 | protected $casts = [ |
||||
29 | 'deleted_at' => 'datetime', |
||||
30 | 'birthday' => 'datetime', |
||||
31 | 'deathday' => 'datetime', |
||||
32 | 'burial_day' => 'datetime', |
||||
33 | 'chan' => 'datetime', |
||||
34 | ]; |
||||
35 | |||||
36 | protected $guarded = ['id']; |
||||
37 | |||||
38 | // protected $fillable = [ |
||||
39 | // 'gid', |
||||
40 | // 'givn', |
||||
41 | // 'surn', |
||||
42 | // 'sex', |
||||
43 | // 'child_in_family_id', |
||||
44 | // 'description', |
||||
45 | // 'title', 'name', 'appellative', 'uid', 'email', 'phone', 'birthday', |
||||
46 | // 'deathday', 'burial_day', 'bank', 'bank_account', |
||||
47 | // 'uid', 'chan', 'rin', 'resn', 'rfn', 'afn', |
||||
48 | // ]; |
||||
49 | protected $fillable = [ |
||||
50 | 'gid', |
||||
51 | 'givn', |
||||
52 | 'surn', |
||||
53 | 'sex', |
||||
54 | 'child_in_family_id', |
||||
55 | 'description', |
||||
56 | 'titl', 'name', 'appellative', 'email', 'phone', 'birthday', |
||||
57 | 'deathday', 'burial_day', 'bank', 'bank_account', 'chan', 'rin', 'resn', 'rfn', 'afn', |
||||
58 | ]; |
||||
59 | |||||
60 | // public function __construct(array $attributes = []) |
||||
61 | // { |
||||
62 | // parent::__construct($attributes); |
||||
63 | // // $this->setConnection(\Session::get('conn')); |
||||
64 | // // error_log('Person-'.($this->connection).'-'.\Session::get('conn').'-'.\Session::get('db')); |
||||
65 | // } |
||||
66 | |||||
67 | public function events() |
||||
68 | { |
||||
69 | return $this->hasMany(PersonEvent::class); |
||||
70 | } |
||||
71 | |||||
72 | public function child_in_family() |
||||
73 | { |
||||
74 | return $this->belongsTo(Family::class, 'child_in_family_id'); |
||||
75 | } |
||||
76 | |||||
77 | public function husband_in_family() |
||||
78 | { |
||||
79 | return $this->hasMany(Family::class, 'husband_id'); |
||||
80 | } |
||||
81 | |||||
82 | public function wife_in_family() |
||||
83 | { |
||||
84 | return $this->hasMany(Family::class, 'wife_id'); |
||||
85 | } |
||||
86 | |||||
87 | public function fullname(): string |
||||
88 | { |
||||
89 | return $this->givn.' '.$this->surn; |
||||
0 ignored issues
–
show
|
|||||
90 | } |
||||
91 | |||||
92 | public function getSex(): string |
||||
93 | { |
||||
94 | if ($this->sex === 'F') { |
||||
0 ignored issues
–
show
|
|||||
95 | return 'Female'; |
||||
96 | } |
||||
97 | |||||
98 | return 'Male'; |
||||
99 | } |
||||
100 | |||||
101 | public static function getList() |
||||
102 | { |
||||
103 | $persons = self::get(); |
||||
104 | $result = []; |
||||
105 | foreach ($persons as $person) { |
||||
106 | $result[$person->id] = $person->fullname(); |
||||
107 | } |
||||
108 | |||||
109 | return collect($result); |
||||
0 ignored issues
–
show
$result of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
110 | } |
||||
111 | |||||
112 | public function addEvent($title, $date, $place, $description = '') |
||||
113 | { |
||||
114 | $place_id = Place::getIdByTitle($place); |
||||
115 | $event = PersonEvent::updateOrCreate( |
||||
116 | [ |
||||
117 | 'person_id' => $this->id, |
||||
118 | 'title' => $title, |
||||
119 | ], |
||||
120 | [ |
||||
121 | 'person_id' => $this->id, |
||||
122 | 'title' => $title, |
||||
123 | 'description' => $description, |
||||
124 | ] |
||||
125 | ); |
||||
126 | |||||
127 | if ($date) { |
||||
128 | $event->date = $date; |
||||
0 ignored issues
–
show
The property
date does not exist on App\Models\PersonEvent . Since you implemented __set , consider adding a @property annotation.
![]() |
|||||
129 | $event->save(); |
||||
130 | } |
||||
131 | |||||
132 | if ($place) { |
||||
133 | $event->places_id = $place_id; |
||||
0 ignored issues
–
show
The property
places_id does not exist on App\Models\PersonEvent . Since you implemented __set , consider adding a @property annotation.
![]() |
|||||
134 | $event->save(); |
||||
135 | } |
||||
136 | |||||
137 | // add birthyear to person table ( for form builder ) |
||||
138 | if ($title === 'BIRT' && ! empty($date)) { |
||||
139 | $this->birthday = date('Y-m-d', strtotime((string) $date)); |
||||
0 ignored issues
–
show
|
|||||
140 | } |
||||
141 | // add deathyear to person table ( for form builder ) |
||||
142 | if ($title === 'DEAT' && ! empty($date)) { |
||||
143 | $this->deathday = date('Y-m-d', strtotime((string) $date)); |
||||
0 ignored issues
–
show
|
|||||
144 | } |
||||
145 | $this->save(); |
||||
146 | |||||
147 | return $event; |
||||
148 | } |
||||
149 | |||||
150 | public function birth() |
||||
151 | { |
||||
152 | return $this->events->where('title', '=', 'BIRT')->first(); |
||||
153 | } |
||||
154 | |||||
155 | public function death() |
||||
156 | { |
||||
157 | return $this->events->where('title', '=', 'DEAT')->first(); |
||||
158 | } |
||||
159 | |||||
160 | public static function bootUpdatedBy() |
||||
161 | { |
||||
162 | self::creating(fn ($model) => $model->setUpdatedBy()); |
||||
163 | |||||
164 | self::updating(fn ($model) => $model->setUpdatedBy()); |
||||
165 | } |
||||
166 | |||||
167 | public function setUpdatedBy() |
||||
168 | { |
||||
169 | if (! is_dir(storage_path('app/public'))) { |
||||
170 | // dir doesn't exist, make it |
||||
171 | \File::makeDirectory(storage_path().'/app/public', 0777, true); |
||||
172 | |||||
173 | // mkdir(storage_path('app/public/'), 0700); |
||||
174 | } |
||||
175 | |||||
176 | file_put_contents(storage_path('app/public/file.txt'), $this->connection); |
||||
177 | if ($this->connection !== 'tenant' && Auth::check()) { |
||||
178 | $this->updated_by = Auth::id(); |
||||
0 ignored issues
–
show
|
|||||
179 | } |
||||
180 | } |
||||
181 | } |
||||
182 |