Group spendings by month on spendings' index view
MODIFY
app/Http/Controllers/SpendingController.php
app/Http/Controllers/SpendingController.php
@@ -11,15 +11,18 @@ use Auth;
class SpendingController extends Controller {
public function index(Request $request) {
- $user = Auth::user();
+ $spendingsByMonth = [];
- $spendings = session('space')
+ for ($month = 12; $month >= 1; $month --) {
+ $spendingsByMonth[$month] = session('space')
->spendings()
+ ->whereYear('happened_on', date('Y'))
+ ->whereMonth('happened_on', $month)
->orderBy('happened_on', 'DESC')
- ->orderBy('created_at', 'DESC')
->get();
+ }
- return view('spendings.index', compact('spendings'));
+ return view('spendings.index', compact('spendingsByMonth'));
}
public function create() {

MODIFY
resources/assets/sass/components/typography.scss
resources/assets/sass/components/typography.scss
@@ -35,6 +35,10 @@ h2 {
font-size: 1.25em;
}
+h3 {
+ font-size: 1em;
+}
+
a {
text-decoration: none;
color: $colors-primary;

MODIFY
resources/views/spendings/index.blade.php
resources/views/spendings/index.blade.php
@section('body')
<div class="wrapper my-3">
<h2>{{ __('general.spendings') }}</h2>
- <div class="box mt-3">
+ @foreach ($spendingsByMonth as $index => $spendings)
@if (count($spendings))
+ <h3 class="mt-3 mb-2">{{ __('calendar.months.' . $index) }}</h3>
+ <div class="box">
@foreach ($spendings as $spending)
<div class="box__section row">
<div class="row__column">
</div>
</div>
@endforeach
- @else
- <div class="box__section text-center">You don't have any spendings</div>
- @endif
</div>
+ @endif
+ @endforeach
</div>
@endsection

Group spendings by month on spendings' index view