Fix some relationship inconsistencies and reduce dashboard queries (#209)

This normalizes all relationships and adds relations to models to take advantage of laravel's eagerloading and reduce queries to the database.

This might go against the 'add any kind of complexities whatsoever' rule in the contribution guide, but I think adding relations would eventually simplify the code base. The amount of queries just on the dashboard fell from ~70 to 26. There's also a lot of boilerplate code in the Contact model to retrieve relationships and to maintain accurate count of them (fields like number_of_*). They can be greatly simplified with the addition of relations.

One other thing in the Contact model was the definition of the Contact-User relation. This definition uses the account_id field in the contacts table and and relates it to id in users. I'm not sure if this is the intention, since the id from accounts table is used when assigning account_id for a contact. This seems to have worked so far, because both a user and its associated account are created at the same time during registration and their ids match, but if there was any difference, this relationship would relate wrong contacts and users. I changed the relationship name to account. This makes the path to a user from a contact is $contact->account->user right now.
This commit is contained in:
Yamamoto Kadir
2017-06-12 12:01:23 +09:00
committed by Régis Freyd
parent beb71095b1
commit 50005544ee
19 changed files with 614 additions and 327 deletions
+49 -77
View File
@@ -23,100 +23,72 @@ class DashboardController extends Controller
*/
public function index()
{
$lastUpdatedContacts = Contact::where('account_id', Auth::user()->account_id)
->orderBy('updated_at', 'desc')
->limit(10)
->get();
$account = Auth::user()->account;
$lastUpdatedContacts = $account->contacts()->limit(10)->get();
// Latest statistics
$contacts = Contact::where('account_id', Auth::user()->account_id)
->get();
if (count($contacts) == 0) {
if ($account->contacts()->count() === 0) {
return view('dashboard.blank');
}
$number_of_contacts = $contacts->count();
$number_of_reminders = 0;
$number_of_notes = 0;
$number_of_activities = 0;
$number_of_gifts = 0;
$number_of_tasks = 0;
$number_of_kids = 0;
$debt_owed = 0;
$debt_due = 0;
$number_of_contacts = $account->contacts()->count();
$number_of_reminders = $account->reminders()->count();
$number_of_notes = $account->notes()->count();
$number_of_activities = $account->activities()->count();
$number_of_gifts = $account->gifts()->count();
$number_of_tasks = $account->tasks()->count();
$number_of_kids = $account->kids()->count();
foreach ($contacts as $contact) {
$number_of_reminders = $number_of_reminders + $contact->number_of_reminders;
$number_of_notes = $number_of_notes + $contact->number_of_notes;
$number_of_activities = $number_of_activities + $contact->number_of_activities;
$number_of_gifts = $number_of_gifts + $contact->number_of_gifts_ideas;
$number_of_gifts = $number_of_gifts + $contact->number_of_gifts_received;
$number_of_gifts = $number_of_gifts + $contact->number_of_gifts_offered;
$number_of_tasks = $number_of_tasks + $contact->number_of_tasks_in_progress;
$number_of_tasks = $number_of_tasks + $contact->number_of_tasks_completed;
$number_of_kids = $number_of_kids + $contact->number_of_kids;
}
$debt = $account->debt->where('status', 'inprogress');
$debts = Debt::where('account_id', Auth::user()->account_id)
->where('status', 'inprogress')
->where('in_debt', 'yes')
->get();
$debt_due = $debt->where('in_debt', 'yes')
->reduce(function ($totalDueDebt, Debt $debt) {
return $totalDueDebt + $debt->amount;
}, 0);
foreach ($debts as $debt) {
$debt_due = $debt_due + $debt->amount;
}
$debts = Debt::where('account_id', Auth::user()->account_id)
->where('status', 'inprogress')
->where('in_debt', 'no')
->get();
foreach ($debts as $debt) {
$debt_owed = $debt_owed + $debt->amount;
}
$debt_owed = $debt->where('in_debt', 'no')
->reduce(function ($totalOwedDebt, Debt $debt) {
return $totalOwedDebt + $debt->amount;
}, 0);
// List of events
$events = Event::where('account_id', Auth::user()->account_id)
->orderBy('created_at', 'desc')
->limit(30)
->get();
$events = $account->events()->with('contact.significantOthers', 'contact.kids')->limit(30)->get()
->reject(function (Event $event) {
return $event->contact === null;
})
->map(function (Event $event) use ($account) {
$eventsArray = collect();
if ($event->object_type === 'significantother') {
$object = $event->contact->significantOthers->where('id', $event->object_id)->first();
} elseif ($event->object_type === 'kid') {
$object = $event->contact->kids->where('id', $event->object_id)->first();
}
foreach ($events as $event) {
$contact = Contact::findOrFail($event->contact_id);
$eventsArray->push([
'id' => $event->id,
'date' => DateHelper::createDateFromFormat($event->created_at, Auth::user()->timezone),
'object_type' => $event->object_type,
'object_id' => $event->object_id,
'contact_id' => $contact->id,
'contact_complete_name' => $contact->getCompleteName(),
'nature_of_operation' => $event->nature_of_operation,
]);
}
return [
'id' => $event->id,
'date' => DateHelper::createDateFromFormat($event->created_at, Auth::user()->timezone),
'object' => $object ?? null,
'object_type' => $event->object_type,
'object_id' => $event->object_id,
'contact_id' => $event->contact->id,
'contact_complete_name' => $event->contact->getCompleteName(),
'nature_of_operation' => $event->nature_of_operation,
];
});
// List of upcoming reminders
$upcomingReminders = Reminder::where('account_id', Auth::user()->account_id)
->where('next_expected_date', '>', Carbon::now())
->orderBy('next_expected_date', 'asc')
->limit(10)
->get();
$upcomingReminders = $account->reminders()
->where('next_expected_date', '>', Carbon::now())
->orderBy('next_expected_date', 'asc')
->limit(10)
->get();
// Active tasks
$tasks = Task::where('account_id', Auth::user()->account_id)
->where('status', 'inprogress')
->get();
// Debts
$debts = Debt::where('account_id', Auth::user()->account_id)
->where('status', 'inprogress')
->get();
$tasks = $account->tasks->where('status', 'inprogress');
$data = [
'events' => $eventsArray,
'events' => $events,
'lastUpdatedContacts' => $lastUpdatedContacts,
'upcomingReminders' => $upcomingReminders,
'number_of_contacts' => $number_of_contacts,
@@ -129,7 +101,7 @@ class DashboardController extends Controller
'debt_due' => $debt_due,
'debt_owed' => $debt_owed,
'tasks' => $tasks,
'debts' => $debts,
'debts' => $debt,
];
return view('dashboard.index', $data);