|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class Family extends Model |
|
8
|
|
|
{ |
|
9
|
|
|
protected $fillable = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id', 'chan', 'nchi', 'rin']; |
|
10
|
|
|
|
|
11
|
|
|
protected $attributes = ['is_active' => false]; |
|
12
|
|
|
|
|
13
|
|
|
protected $casts = ['is_active' => 'boolean']; |
|
14
|
|
|
|
|
15
|
|
|
public function events() |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->hasMany(FamilyEvent::class); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function children() |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->hasMany(Person::class, 'child_in_family_id'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function husband() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->hasOne(Person::class, 'id', 'husband_id'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function wife() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->hasOne(Person::class, 'id', 'wife_id'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function title() |
|
36
|
|
|
{ |
|
37
|
|
|
return ($this->husband ? $this->husband->fullname() : '?'). |
|
|
|
|
|
|
38
|
|
|
' + '. |
|
39
|
|
|
($this->wife ? $this->wife->fullname() : '?'); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function getList() |
|
43
|
|
|
{ |
|
44
|
|
|
$families = self::query()->get(); |
|
45
|
|
|
$result = []; |
|
46
|
|
|
foreach ($families as $family) { |
|
47
|
|
|
$result[$family->id] = $family->title(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return collect($result); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function addEvent($title, $date, $place, $description = '') |
|
54
|
|
|
{ |
|
55
|
|
|
$place_id = Place::getIdByTitle($place); |
|
56
|
|
|
$event = FamilyEvent::query()->updateOrCreate( |
|
57
|
|
|
[ |
|
58
|
|
|
'family_id' => $this->id, |
|
59
|
|
|
'title' => $title, |
|
60
|
|
|
], |
|
61
|
|
|
[ |
|
62
|
|
|
'family_id' => $this->id, |
|
63
|
|
|
'title' => $title, |
|
64
|
|
|
'description' => $description, |
|
65
|
|
|
] |
|
66
|
|
|
); |
|
67
|
|
|
if ($date) { |
|
68
|
|
|
$event->date = $date; |
|
69
|
|
|
$event->save(); |
|
70
|
|
|
} |
|
71
|
|
|
if ($place) { |
|
72
|
|
|
$event->places_id = $place_id; |
|
73
|
|
|
$event->save(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getWifeName() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->wife ? $this->wife->fullname() : 'unknown woman'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getHusbandName() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->husband ? $this->husband->fullname() : 'unknown man'; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|