Last active
May 12, 2018 11:57
-
-
Save BentBr/b3e47e5de814f756ab458d75b71f4adf to your computer and use it in GitHub Desktop.
Eloquent Model for a family tree.One can find parents, children and partner through relations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SaveMyData\Models; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent; | |
use Illuminate\Support\Carbon; | |
use Illuminate\Support\Collection; | |
class Member extends Model | |
{ | |
use SoftDeletes; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'first_name', | |
'last_name', | |
'nick_name', | |
'birthday', | |
'sex_id', | |
'user_id', | |
'obit', | |
'mother_id', | |
'father_id', | |
]; | |
private $parent; | |
private $kids; | |
/** | |
* Getting parents if available | |
* | |
* Member constructor. | |
*/ | |
public function __construct() | |
{ | |
if (! is_null($this->mother_id)) { | |
$this->mother->with('mother'); | |
} | |
if (! is_null($this->father_id)) { | |
$this->father->with('father'); | |
} | |
} | |
/** | |
* @return Eloquent\Relations\BelongsToMany | |
*/ | |
public function family() | |
{ | |
return $this->belongsToMany('\SaveMyData\Models\Family', 'family_member', 'member_id', 'family_id') | |
->withPivot('is_member_by_heredity'); | |
} | |
/** | |
* @return Eloquent\Relations\BelongsTo | |
*/ | |
public function user() | |
{ | |
return $this->belongsTo('\SaveMyData\Models\User'); | |
} | |
/** | |
* @return Eloquent\Relations\BelongsTo | |
*/ | |
public function sex() | |
{ | |
return $this->belongsTo('\SaveMyData\Models\Sex'); | |
} | |
/** | |
* @return Eloquent\Relations\BelongsTo | |
*/ | |
public function mother() | |
{ | |
$mother = $this->belongsTo('\SaveMyData\Models\Member', 'mother_id'); | |
if (isset($mother->kids)) { | |
$mother->kids->merge($mother); | |
} | |
return $mother; | |
} | |
/** | |
* @return Eloquent\Relations\BelongsTo | |
*/ | |
public function father() | |
{ | |
$father = $this->belongsTo('\SaveMyData\Models\Member', 'father_id'); | |
if (isset($father->kids)) { | |
$father->kids->merge($father); | |
} | |
return $father; | |
} | |
/** | |
* Returns all children of member. Determines if $this member is female or male | |
* | |
* @return Eloquent\Relations\HasMany | |
*/ | |
public function children() | |
{ | |
$foreignKey = 'mother_id'; | |
if ($this->sex_id == 1) { | |
$foreignKey = 'father_id'; | |
} | |
$children = $this->hasMany('\SaveMyData\Models\Member', $foreignKey); | |
foreach ($children as $child) { | |
$child->parent = $this; | |
} | |
return $children; | |
} | |
/** | |
* @return Eloquent\Relations\BelongsToMany | |
*/ | |
public function relation() | |
{ | |
return $this->belongsToMany('\SaveMyData\Models\Relation', 'relation_member', 'member_id', 'relation_id'); | |
} | |
/** | |
* Returns a collection of members (which are partners due to relations) | |
* | |
* @return Collection | |
*/ | |
public function partner() | |
{ | |
$relations = $this->relation()->get(); | |
$partners = new Collection(); | |
foreach ($relations as $relation) { | |
foreach ($relation->member as $partner) { | |
if ($partner->id != $this->id) { | |
$partners->push($partner); | |
} | |
} | |
} | |
return $partners; | |
} | |
/** | |
* Returns nick_name if is filled. Otherwise first_name | |
* | |
* @return mixed | |
*/ | |
public function name() | |
{ | |
$first_name = $this->first_name; | |
$nick_name = $this->nick_name; | |
if (!is_null($nick_name)) { | |
return $nick_name; | |
} else { | |
return $first_name; | |
} | |
} | |
/** | |
* Returns age of member | |
* | |
* @return int | |
*/ | |
public function age() | |
{ | |
$age = Carbon::parse($this->birthday)->age; | |
return $age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment