Changed functions in Person model such that they return a Relation instead of an

Eloquent collection and adjusted the PersonController accordingly.
This commit is contained in:
Lukas Halbritter
2018-09-11 17:11:46 +02:00
parent 003ff1435a
commit 5ec117e1af
2 changed files with 19 additions and 23 deletions
+16 -20
View File
@@ -21,7 +21,7 @@ class Person extends Model
*/
public function isMember()
{
$latest = $this->getMemberStates()->last();
$latest = $this->memberStates->last();
if (is_null($latest)) {
return false;
@@ -39,7 +39,7 @@ class Person extends Model
*/
public function wasMember()
{
return !($this->getMemberStates()->isEmpty());
return !($this->memberStates->isEmpty());
}
/*
@@ -49,13 +49,9 @@ class Person extends Model
*/
public static function active()
{
return Person::join('member_states', 'people.id', '=', 'member_states.person_id')
->where('member_states.leaving_date', null)
->select('person_id as id', 'title', 'name_or_company',
'surname', 'date_of_birth', 'city', 'street',
'postal_code', 'email', 'mobile', 'phone',
'joining_date', 'leaving_date')
->get();
return Person::whereHas('memberStates', function($memberState) {
$memberState->where('leaving_date', '=', null);
})->get();
}
/*
@@ -65,7 +61,7 @@ class Person extends Model
*/
public function activeSince()
{
$latest = $this->hasMany('\App\MemberState')
$latest = $this->memberStates
->orderBy('joining_date', 'desc')
->first();
@@ -79,7 +75,7 @@ class Person extends Model
*/
public function activeTime()
{
$activeTimes = $this->hasMany('\App\MemberState')->get();
$activeTimes = $this->memberStates;
$diff = 0;
@@ -101,11 +97,11 @@ class Person extends Model
/*
* Get all member state records associated with a person.
*
* @return Illuminate\Database\Eloquent\Collection
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getMemberStates()
public function memberStates()
{
return $this->hasMany('\App\MemberState')->orderBy('joining_date')->get();
return $this->hasMany('\App\MemberState')->orderBy('joining_date');
}
/**************************************************************************
@@ -115,20 +111,20 @@ class Person extends Model
/*
* Get all targets associated with a person.
*
* @return Illuminate\Database\Eloquent\Collection
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getTargets()
public function targets()
{
return $this->hasMany('\App\PersonTarget')->orderBy('date')->get();
return $this->hasMany('\App\PersonTarget')->orderBy('date');
}
/*
* Get all actuals associated with a person.
*
* @return Illuminate\Database\Eloquent\Collection
* @return Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function getActuals()
public function actuals()
{
return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget')->get();
return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget');
}
}