Implemented eager loading for the people show view.

This commit is contained in:
Lukas Halbritter
2018-10-01 09:58:04 +02:00
parent 8f865e9bff
commit e9077193af
4 changed files with 164 additions and 167 deletions
+4 -5
View File
@@ -20,12 +20,11 @@ class PeopleController extends Controller
public function show(Person $person)
{
$memberStates = $person->memberStates;
$targets = $person->targets;
$actuals = $person->actuals;
$person->load('targets.actuals.accountType.accountClass.ioType',
'targets.actuals.booking',
'targets.ioType');
return view('people.show', compact('person', 'memberStates', 'targets',
'actuals'));
return view('people.show', compact('person'));
}
// Show the form for creating a new person
+80 -76
View File
@@ -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.
+76 -82
View File
@@ -7,6 +7,82 @@ use Illuminate\Support\Facades\DB;
class PersonTarget extends Model
{
/**************************************************************************
* DIRECT RELATIONS *
**************************************************************************/
/**
* Get the IOType of the target.
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function ioType()
{
return $this->belongsTo('App\IOType');
}
/**
* Get the actuals connected with the target.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function actuals()
{
return $this->hasMany('App\SubBooking');
}
/**************************************************************************
* INDIRECT RELATIONS *
**************************************************************************/
/**************************************************************************
* INDIRECT PROPERTIES *
**************************************************************************/
/**
* Get the signed amount of the target.
*
* @return float
*/
public function signedAmount()
{
return $this->amount * $this->ioType->factor;
}
/**
* Return the sum of all actuals belonging to this target
*
* @return float
*/
public function actualsSum() {
$sum = 0;
foreach ($this->actuals as $actual) {
$sum += $actual->signedAmount();
}
return $sum;
}
/**
* Get the difference of target and actually payed amount so far. I. e.
* return the amount still to be payed.
*
* @return float
*/
public function difference() {
return $this->signedAmount() - $this->actualsSum();
}
/**
* Return true, if the target is paid and therefore closed.
*
* @return boolean
*/
public function isPaid() {
return $this->difference() == 0;
}
/**************************************************************************
* COLLECTIONS FILTERED BY PAYMENT STATES *
@@ -130,86 +206,4 @@ class PersonTarget extends Model
// Create a collection of PersonTargets from the raw query result.
return PersonTarget::hydrate($selection);
}
/**************************************************************************
* BOOKINGS *
**************************************************************************/
/*
* Get all bookings associated with a target.
*
* @return Illuminate\Database\Eloquent\Collection
*/
public function bookings()
{
return $this->hasMany('\App\SubBooking');
}
/**************************************************************************
* IO TYPES *
**************************************************************************/
/*
* Get the IOType of the target.
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function ioType()
{
return $this->belongsTo('App\IOType');
}
/**************************************************************************
* TARGET AND ACTUAL VALUES *
**************************************************************************/
/*
* Get the signed amount of the target.
*
* @return float
*/
public function signedAmount()
{
return $this->amount * $this->ioType->factor;
}
/*
* Get the actuals connected with the target.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function actuals()
{
return $this->hasMany('App\SubBooking');
}
/*
* Return the sum of all actuals belonging to this target
*
* @return float
*/
public function actualsSum() {
return $this->actuals->each(function ($item, $key) {
$item['signedAmount'] = $item->signedAmount();
})->sum('signedAmount');
}
/*
* Get the difference of target and actually payed amount so far. I. e.
* return the amount still to be payed.
*
* @return float
*/
public function difference() {
return $this->signedAmount() - $this->actualsSum();
}
/*
* Return true, if the target is paid and therefore closed.
*
* @return boolean
*/
public function isPaid() {
return $this->difference() == 0;
}
}