Add year/month arguments to dashboard route
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -7,24 +7,56 @@ use Illuminate\Http\Request;
use Auth;
class DashboardController extends Controller {
- public function index() {
- $user = Auth::user();
-
+ public function index($year = null, $month = null) {
+ if (!$year && !$month) {
$year = date('Y');
+
$month = date('n');
+ }
+
+ $previousYear = $year;
+ $previousMonth = $month - 1;
+ $nextYear = $year;
+ $nextMonth = $month + 1;
+
+ if ($previousMonth < 1) {
+ $previousYear --;
+ $previousMonth = 12;
+ } else if ($nextMonth > 12) {
+ $nextYear ++;
+ $nextMonth = 1;
+ }
+
+ $user = Auth::user();
$currency = $user->currency;
$earnings = $user->earnings()
->whereYear('date', $year)
->whereMonth('date', $month)
- ->sum('amount');
+ ->get();
$spendings = $user->spendings()
->whereYear('date', $year)
->whereMonth('date', $month)
- ->sum('amount');
-
- return view('dashboard.index', compact('currency', 'month', 'earnings', 'spendings'));
+ ->get();
+
+ $budgets = $user->budgets()
+ ->where('year', $year)
+ ->where('month', $month)
+ ->get();
+
+ return view('dashboard.index', compact(
+ 'year',
+ 'month',
+ 'previousYear',
+ 'nextYear',
+ 'previousMonth',
+ 'nextMonth',
+ 'currency',
+ 'earnings',
+ 'spendings',
+ 'budgets'
+ ));
}
}

MODIFY
public/style.css
public/style.css
@@ -28,11 +28,11 @@ a:hover {
color: #4880FE;
}
-body, h1, h2, th {
+body, h1, h2, h3, th {
font-weight: 400;
}
-h1, h2 {
+h1, h2, h3 {
color: #18223C;
}
@@ -44,6 +44,10 @@ h2 {
font-size: 24px;
}
+h3 {
+ font-size: 18px;
+}
+
/* Foundation */
body {
@@ -119,6 +123,10 @@ body {
margin: 0 auto;
}
+.row .column.align-center {
+ text-align: center;
+}
+
.row .column.align-middle {
align-self: center;
}

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

MODIFY
routes/web.php
routes/web.php
@@ -7,7 +7,7 @@ Route::get('/register', 'RegisterController@index');
Route::post('/register', 'RegisterController@store');
Route::group(['middleware' => ['auth']], function () {
- Route::get('/dashboard', 'DashboardController@index')->name('dashboard.index');
+ Route::get('/dashboard/{year?}/{month?}', 'DashboardController@index')->name('dashboard.index');
Route::get('/search', 'SearchController@index');

Add year/month arguments to dashboard route