You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
2.9 KiB
135 lines
2.9 KiB
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'nickname', 'gender_id', 'name',
|
|
'email', 'password',
|
|
'address', 'phone',
|
|
'dof', 'dod',
|
|
'father_id', 'mother_id', 'parent_id',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password', 'remember_token',
|
|
];
|
|
|
|
protected $appends = [
|
|
'gender',
|
|
];
|
|
|
|
public function getGenderAttribute()
|
|
{
|
|
return $this->gender_id == 1 ? 'Laki-laki' : 'Perempuan';
|
|
}
|
|
|
|
public function setFather(User $father)
|
|
{
|
|
if ($father->gender_id == 1) {
|
|
|
|
if ($father->exists == false)
|
|
$father->save();
|
|
|
|
$this->father_id = $father->id;
|
|
$this->save();
|
|
|
|
return $father;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function setMother(User $mother)
|
|
{
|
|
if ($mother->gender_id == 2) {
|
|
|
|
if ($mother->exists == false)
|
|
$mother->save();
|
|
|
|
$this->mother_id = $mother->id;
|
|
$this->save();
|
|
|
|
return $mother;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function father()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function mother()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function childs()
|
|
{
|
|
if ($this->gender_id == 2)
|
|
return $this->hasMany(User::class, 'mother_id');
|
|
|
|
return $this->hasMany(User::class, 'father_id');
|
|
}
|
|
|
|
public function profileLink()
|
|
{
|
|
return link_to_route('users.show', $this->name, [$this->id]);
|
|
}
|
|
|
|
public function wifes()
|
|
{
|
|
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps();
|
|
}
|
|
|
|
public function addWife(User $wife)
|
|
{
|
|
if ($this->gender_id == 1) {
|
|
$this->wifes()->save($wife);
|
|
return $wife;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function husbands()
|
|
{
|
|
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps();
|
|
}
|
|
|
|
public function addHusband(User $husband)
|
|
{
|
|
if ($this->gender_id == 2) {
|
|
$this->husbands()->save($husband);
|
|
return $husband;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function marriages()
|
|
{
|
|
if ($this->gender_id == 1)
|
|
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps();
|
|
|
|
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps();
|
|
}
|
|
}
|