From e9077193af93839c8dc4c24be806572b6d645ca7 Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Mon, 1 Oct 2018 09:58:04 +0200 Subject: [PATCH] Implemented eager loading for the people show view. --- app/Http/Controllers/PeopleController.php | 9 +- app/Person.php | 156 ++++++++++----------- app/PersonTarget.php | 158 +++++++++++----------- resources/views/people/show.blade.php | 8 +- 4 files changed, 164 insertions(+), 167 deletions(-) diff --git a/app/Http/Controllers/PeopleController.php b/app/Http/Controllers/PeopleController.php index f14236f..86cdb40 100644 --- a/app/Http/Controllers/PeopleController.php +++ b/app/Http/Controllers/PeopleController.php @@ -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 diff --git a/app/Person.php b/app/Person.php index 344b720..8b6dfcf 100644 --- a/app/Person.php +++ b/app/Person.php @@ -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. diff --git a/app/PersonTarget.php b/app/PersonTarget.php index 036cd56..c235608 100644 --- a/app/PersonTarget.php +++ b/app/PersonTarget.php @@ -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; - } } diff --git a/resources/views/people/show.blade.php b/resources/views/people/show.blade.php index 2b17d7b..d8024f1 100644 --- a/resources/views/people/show.blade.php +++ b/resources/views/people/show.blade.php @@ -105,13 +105,13 @@ {{ formatAmount($target->actualsSum()) }} {{ formatAmount($target->difference()) }} - @foreach ($target->bookings as $booking) + @foreach ($target->actuals as $actual) - {{ $booking->date() }} - {{ $booking->reference }} + {{ $actual->date() }} + {{ $actual->reference }} - {{ formatAmount($booking->signedAmount()) }} + {{ formatAmount($actual->signedAmount()) }} @endforeach