Get rid of all in-view queries for ReportsController and (temporarily) remove alerts from reports's show view
MODIFY
app/Http/Controllers/ReportsController.php
app/Http/Controllers/ReportsController.php
@@ -4,15 +4,43 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
+use App\Earning;
+use App\Spending;
+use App\Budget;
+
+use Auth;
+
class ReportsController extends Controller {
public function index() {
- return view('reports.index');
+ $user = Auth::user();
+
+ $budgets = Budget::where('user_id', $user->id)
+ ->groupBy('year', 'month')
+ ->get(['id', 'year', 'month']);
+
+ return view('reports.index', compact('budgets'));
}
public function show($year, $month) {
- return view('reports.show', [
- 'year' => $year,
- 'month' => $month
- ]);
+ $user = Auth::user();
+
+ $currency = view('partials.currency');
+
+ $earnings = Earning::where('user_id', $user->id)
+ ->whereYear('date', $year)
+ ->whereMonth('date', $month)
+ ->sum('amount');
+
+ $spendings = Spending::where('user_id', $user->id)
+ ->whereYear('date', $year)
+ ->whereMonth('date', $month)
+ ->sum('amount');
+
+ $budgets = Budget::where('user_id', $user->id)
+ ->where('year', $year)
+ ->where('month', $month)
+ ->get();
+
+ return view('reports.show', compact('year', 'month', 'currency', 'earnings', 'spendings', 'budgets'));
}
}

MODIFY
config/database.php
config/database.php
@@ -50,7 +50,7 @@ return [
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
- 'strict' => true,
+ 'strict' => false,
'engine' => null,
],

MODIFY
resources/views/reports/index.blade.php
resources/views/reports/index.blade.php
<div class="box">
<table class="box__section">
<tbody>
- @for ($y = 2017; $y <= date('Y'); $y ++)
- @for ($m = 1; $m <= 12; $m ++)
- @if (App\Budget::where('user_id', Auth::user()->id)->whereMonth('date', $m)->whereYear('date', $y)->count())
+ @foreach ($budgets as $budget)
<tr>
<td>
- <a href="/reports/{{ $y }}/{{ $m }}">@lang('months.' . $m), {{ $y }}</a>
+ <a href="/reports/{{ $budget->year }}/{{ $budget->month }}">@lang('months.' . $budget->month), {{ $budget->year }}<a>
</td>
</tr>
- @endif
- @endfor
- @endfor
+ @endforeach
</tbody>
</table>
</div>

MODIFY
resources/views/reports/show.blade.php
resources/views/reports/show.blade.php
<div class="row__column">
<div class="box">
<div class="box__section box__section--align-center">
- <h2 class="spacing-bottom-small">@include('partials.currency') {{ App\Earning::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->sum('amount') }}</h2>
+ <h2 class="spacing-bottom-small">{{ $currency }} {{ $earnings }}</h2>
<p>Earned</p>
</div>
</div>
<div class="row__column">
<div class="box">
<div class="box__section box__section--align-center">
- <h2 class="spacing-bottom-small">@include('partials.currency') {{ App\Spending::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->sum('amount') }}</h2>
+ <h2 class="spacing-bottom-small">{{ $currency }} {{ $spendings }}</h2>
<p>Spent</p>
</div>
</div>
<div class="row__column">
<div class="box">
<div class="box__section box__section--align-center">
- <h2 class="spacing-bottom-small">@include('partials.currency') {{ App\Earning::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->sum('amount') - App\Spending::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->sum('amount') }}</h2>
+ <h2 class="spacing-bottom-small">{{ $currency }} {{ $earnings - $spendings }}</h2>
<p>Net</p>
</div>
</div>
</div>
</div>
- <div class="box spacing-bottom-large">
- <div class="box__section">
- <p>Alerts</p>
- </div>
- <table class="box__section">
- <tbody>
- @foreach (App\Budget::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->get() as $budget)
- @if (App\Spending::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->where('tag_id', $budget->tag->id)->sum('amount') > $budget->amount)
- <tr>
- <td>Exceeded budget for '{{ $budget->tag->name }}' by @include('partials.currency') {{ App\Spending::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->where('tag_id', $budget->tag->id)->sum('amount') - $budget->amount }}</td>
- </tr>
- @endif
- @endforeach
- </tbody>
- </table>
- </div>
<div class="box">
<div class="box__section">
<p>Budgets</p>
</div>
<table class="box__section">
<tbody>
- @foreach (App\Budget::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->get() as $budget)
+ @foreach ($budgets as $budget)
<tr>
<td>{{ $budget->tag->name }}</td>
- <td>@include('partials.currency') {{ $budget->spendings()->sum('amount') }} of @include('partials.currency') {{ $budget->amount }}</td>
+ <td>{{ $currency }} {{ $budget->spendings()->sum('amount') }} of {{ $currency }} {{ $budget->amount }}</td>
</tr>
@endforeach
</tbody>

Get rid of all in-view queries for ReportsController and (temporarily) remove alerts from reports's show view