Add total earnings/spendings and balance to dashboard view
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -31,6 +31,18 @@ class DashboardController extends Controller {
$currency = $user->currency;
+ $totalEarnings = $user->earnings()
+ ->whereYear('date', $year)
+ ->whereMonth('date', $month)
+ ->sum('amount');
+
+ $totalSpendings = $user->spendings()
+ ->whereYear('date', $year)
+ ->whereMonth('date', $month)
+ ->sum('amount');
+
+ $balance = $totalEarnings - $totalSpendings;
+
$earnings = $user->earnings()
->whereYear('date', $year)
->whereMonth('date', $month)
@@ -54,6 +66,9 @@ class DashboardController extends Controller {
'previousMonth',
'nextMonth',
'currency',
+ 'totalEarnings',
+ 'totalSpendings',
+ 'balance',
'earnings',
'spendings',
'budgets'

MODIFY
resources/views/dashboard/index.blade.php
resources/views/dashboard/index.blade.php
@extends('layout')
@section('body')
- <div class="row spacing-bottom-small">
- <div class="column align-middle align-right">
- <a href="/dashboard/{{ $previousYear }}/{{ $previousMonth }}">Previous</a>
- </div>
- <div class="column tight align-middle align-center">
- <h3>@lang('months.' . $month)</h3>
- </div>
+ <div class="row">
<div class="column align-middle">
- <a href="/dashboard/{{ $nextYear }}/{{ $nextMonth }}">Next</a>
- </div>
- </div>
<div class="row">
- <div class="column align-middle align-right">
+ <div class="column tight align-middle">
<a href="/dashboard/{{ $year - 1 }}/{{ $month }}">Previous</a>
</div>
- <div class="column tight align-middle align-center">
+ <div class="column tight align-middle">
<h1>{{ $year }}</h1>
</div>
- <div class="column align-middle">
+ <div class="column tight align-middle">
<a href="/dashboard/{{ $year + 1 }}/{{ $month }}">Next</a>
</div>
</div>
+ </div>
+ <div class="column tight align-middle align-right">
+ <div class="row">
+ <div class="column tight align-middle">
+ <a href="/dashboard/{{ $previousYear }}/{{ $previousMonth }}">Previous</a>
+ </div>
+ <div class="column tight align-middle">
+ <h2>@lang('months.' . $month)</h2>
+ </div>
+ <div class="column tight align-middle">
+ <a href="/dashboard/{{ $nextYear }}/{{ $nextMonth }}">Next</a>
+ </div>
+ </div>
+ </div>
+ </div>
<div class="row spacing-top-large">
<div class="column">
- <h2 class="spacing-bottom-medium">Earnings</h2>
+ <div class="box spacing-large">
+ <h1 class="color-green spacing-bottom-small">{{ $currency->symbol }} {{ $totalEarnings }}</h1>
+ <p>Earnings</p>
+ </div>
+ </div>
+ <div class="column">
+ <div class="box spacing-large">
+ <h1 class="color-red spacing-bottom-small">{{ $currency->symbol }} {{ $totalSpendings }}</h1>
+ <p>Spendings</p>
+ </div>
+ </div>
+ <div class="column">
+ <div class="box spacing-large">
+ <h1 class="color-blue spacing-bottom-small">{{ $currency->symbol }} {{ $balance }}</h1>
+ <p>Balance</p>
+ </div>
+ </div>
+ </div>
+ <h2 class="spacing-top-large spacing-bottom-medium">Earnings</h2>
<table class="box spacing-large">
<tbody>
@foreach ($earnings as $earning)
@endforeach
</tbody>
</table>
- </div>
- <div class="column">
- <h2 class="spacing-bottom-medium">Spendings</h2>
+ <h2 class="spacing-top-large spacing-bottom-medium">Spendings</h2>
<table class="box spacing-large">
<tbody>
@foreach ($spendings as $spending)
@endforeach
</tbody>
</table>
- </div>
- <div class="column">
- <h2 class="spacing-bottom-medium">Budgets</h2>
+ <h2 class="spacing-top-large spacing-bottom-medium">Budgets</h2>
<table class="box spacing-large">
<tbody>
@foreach ($budgets as $budget)
@endforeach
</tbody>
</table>
- </div>
- </div>
@endsection

Add total earnings/spendings and balance to dashboard view