Implemented eager loading for the people show view.
This commit is contained in:
+80
-76
@@ -11,10 +11,78 @@ use App\PersonTarget;
|
||||
class Person extends Model
|
||||
{
|
||||
/**************************************************************************
|
||||
* MEMBER STATE FUNCTIONS *
|
||||
* DIRECT RELATIONS *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
/**
|
||||
* Get all member state records associated with a person.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function memberStates()
|
||||
{
|
||||
return $this->hasMany('\App\MemberState')->orderBy('joining_date');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all targets associated with a person.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function targets()
|
||||
{
|
||||
return $this->hasMany('\App\PersonTarget')->orderBy('date');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* INDIRECT RELATIONS *
|
||||
**************************************************************************/
|
||||
|
||||
/**
|
||||
* Get all actuals associated with a person.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function actuals()
|
||||
{
|
||||
return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* INDIRECT PROPERTIES *
|
||||
**************************************************************************/
|
||||
|
||||
/**
|
||||
* Return, if the person is even.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEven()
|
||||
{
|
||||
foreach ($this->targets as $target) {
|
||||
if (!$target->isPaid()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full name of the person.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fullName()
|
||||
{
|
||||
if ($this->title == 'Firma') {
|
||||
return $this->name_or_company;
|
||||
} else {
|
||||
return $this->surname . ' ' . $this->name_or_company;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true, if the person is active member, return false otherwise.
|
||||
*
|
||||
* @return boolean
|
||||
@@ -29,7 +97,7 @@ class Person extends Model
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Returns true, if the person was active member at some time in the past.
|
||||
* Note, that this function also returns true, if the person is still an
|
||||
* active member.
|
||||
@@ -41,19 +109,7 @@ class Person extends Model
|
||||
return !($this->memberStates->isEmpty());
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a collection of all persons which are active members.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function active()
|
||||
{
|
||||
return Person::whereHas('memberStates', function($memberState) {
|
||||
$memberState->where('leaving_date', '=', null);
|
||||
})->get();
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Return the last date since when the person is an active member.
|
||||
*
|
||||
* @return Carbon\Carbon
|
||||
@@ -67,7 +123,7 @@ class Person extends Model
|
||||
return Carbon::parse($latest['joining_date']);
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Returns the active time of the person until now in years.
|
||||
*
|
||||
* @return float
|
||||
@@ -93,68 +149,16 @@ class Person extends Model
|
||||
return $diff;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get all member state records associated with a person.
|
||||
/**
|
||||
* Return a collection of all persons which are active members.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
||||
* @return Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function memberStates()
|
||||
public static function active()
|
||||
{
|
||||
return $this->hasMany('\App\MemberState')->orderBy('joining_date');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* PERSON TARGET FUNCTIONS *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
* Get all targets associated with a person.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function targets()
|
||||
{
|
||||
return $this->hasMany('\App\PersonTarget')->orderBy('date');
|
||||
}
|
||||
|
||||
/*
|
||||
* Get all actuals associated with a person.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function actuals()
|
||||
{
|
||||
return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget');
|
||||
}
|
||||
|
||||
/*
|
||||
* Return, if the person is even.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEven()
|
||||
{
|
||||
foreach ($this->targets as $target) {
|
||||
if (!$target->isPaid()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the full name of the person.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fullName()
|
||||
{
|
||||
if ($this->title == 'Firma') {
|
||||
return $this->name_or_company;
|
||||
} else {
|
||||
return $this->surname . ' ' . $this->name_or_company;
|
||||
}
|
||||
return Person::whereHas('memberStates', function($memberState) {
|
||||
$memberState->where('leaving_date', '=', null);
|
||||
})->get();
|
||||
}
|
||||
|
||||
// Make all attributes mass assignable.
|
||||
|
||||
Reference in New Issue
Block a user