|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use LaravelEnso\People\App\Models\Person; |
|
|
|
|
|
|
7
|
|
|
use LaravelEnso\Tables\App\Traits\TableCache; |
|
8
|
|
|
|
|
9
|
|
|
class Family extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
use TableCache; |
|
12
|
|
|
protected $fillable = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id']; |
|
13
|
|
|
|
|
14
|
|
|
protected $attributes = ['is_active' => false]; |
|
15
|
|
|
|
|
16
|
|
|
protected $casts = ['is_active' => 'boolean']; |
|
17
|
|
|
|
|
18
|
|
|
public function events() |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->hasMany(FamilyEvent::class); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function children() |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->hasMany(Person::class, 'child_in_family_id'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function husband() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->hasOne(Person::class, 'id', 'husband_id'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function wife() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->hasOne(Person::class, 'id', 'wife_id'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function title() |
|
39
|
|
|
{ |
|
40
|
|
|
return (($this->husband) ? $this->husband->fullname() : "?") . |
|
41
|
|
|
" + " . |
|
42
|
|
|
(($this->wife) ? $this->wife->fullname() : "?"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function getList() |
|
46
|
|
|
{ |
|
47
|
|
|
$families = self::get(); |
|
48
|
|
|
$result = array(); |
|
49
|
|
|
foreach ($families as $family) { |
|
50
|
|
|
$result[$family->id] = $family->title(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return collect($result); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function addEvent($title, $date, $place, $description = '') |
|
57
|
|
|
{ |
|
58
|
|
|
$place_id = Place::getIdByTitle($place); |
|
59
|
|
|
$event = FamilyEvent::create(array( |
|
60
|
|
|
'family_id' => $this->id, |
|
61
|
|
|
'title' => $title, |
|
62
|
|
|
'description' => $description, |
|
63
|
|
|
)); |
|
64
|
|
|
if ($date) { |
|
65
|
|
|
$event->date = $date; |
|
66
|
|
|
$event->save(); |
|
67
|
|
|
} |
|
68
|
|
|
if ($place) { |
|
69
|
|
|
$event->places_id = $place_id; |
|
70
|
|
|
$event->save(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
public function getWifeName() |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->wife) { |
|
78
|
|
|
return $this->wife->fullname(); |
|
79
|
|
|
} else { |
|
80
|
|
|
return "unknown woman"; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getHusbandName() |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->husband) { |
|
87
|
|
|
return $this->husband->fullname(); |
|
88
|
|
|
} else { |
|
89
|
|
|
return "unknown man"; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: