Refactor spendings page to simply display list of spendings
MODIFY
app/Http/Controllers/SpendingsController.php
app/Http/Controllers/SpendingsController.php
@@ -13,38 +13,13 @@ class SpendingsController extends Controller {
public function index(Request $request) {
$user = Auth::user();
- $limit = 5;
+ $spendings = $user
+ ->spendings()
+ ->orderBy('happened_on', 'DESC')
+ ->orderBy('created_at', 'DESC')
+ ->get();
- $currentPage = 1;
-
- if ($request->has('page')) {
- $currentPage = $request->get('page');
- }
-
- $sort = null;
-
- $sortColumn = 'date';
- $sortDirection = 'DESC';
-
- if ($request->has('sort')) {
- $sort = $request->get('sort');
-
- $parts = explode('-', $sort);
-
- $sortColumn = $parts[0];
- $sortDirection = $parts[1];
-
- if ($sortColumn == 'price') {
- $sortColumn = 'amount';
- }
- }
-
- return view('spendings.index', [
- 'spendings' => $user->spendings()->orderBy($sortColumn, $sortDirection)->offset($currentPage * $limit - $limit)->limit($limit)->get(),
- 'currentPage' => $currentPage,
- 'totalPages' => ceil($user->spendings->count() / $limit),
- 'sort' => $sort
- ]);
+ return view('spendings.index', compact('spendings'));
}
public function create() {

MODIFY
resources/assets/sass/components/helpers.scss
resources/assets/sass/components/helpers.scss
text-align: center;
}
+.text-right {
+ text-align: right;
+}
+
.mt-1 {
margin-top: 10px;
}

MODIFY
resources/views/spendings/index.blade.php
resources/views/spendings/index.blade.php
<h1>Spendings</h1>
</div>
<div class="wrapper spacing-top-large spacing-bottom-large">
- <div class="spacing-bottom-large text-align-center">
- {!! ($sort == 'date-asc') ? '<span>' : '<a href="/spendings?sort=date-asc">' !!}Sort by Date <i class="fa fa-arrow-up"></i>{!! ($sort == 'date-asc') ? '</span>' : '</a>' !!} &middot;
- {!! ($sort == 'date-desc' || !$sort) ? '<span>' : '<a href="/spendings?sort=date-desc">' !!}Sort by Date <i class="fa fa-arrow-down"></i>{!! ($sort == 'date-desc' || !$sort) ? '</span>' : '</a>' !!} &middot;
- {!! ($sort == 'price-asc') ? '<span>' : '<a href="/spendings?sort=price-asc">' !!}Sort by Price <i class="fa fa-arrow-up"></i>{!! ($sort == 'price-asc') ? '</span>' : '</a>' !!} &middot;
- {!! ($sort == 'price-desc') ? '<span>' : '<a href="/spendings?sort=price-desc">' !!}Sort by Price <i class="fa fa-arrow-down"></i>{!! ($sort == 'price-desc') ? '</span>' : '</a>' !!}
- </div>
<div class="box">
+ @if (count($spendings))
@foreach ($spendings as $spending)
<div class="section row">
- <div class="column">{{ $spending->description }}</div>
- <div class="column">{{ ($spending->tag) ? $spending->tag->name : 'N/A' }}</div>
- <div class="column">&euro; {{ $spending->amount }}</div>
+ <div class="row__column">
+ @if ($spending->tag)
+ <div style="
+ margin-bottom: 10px;
+ display: inline-block;
+ padding: 5px 10px;
+ text-transform: uppercase;
+ font-size: 12px;
+ font-weight: bolder;
+ bolder; color: #FFF;
+ background: #333;
+ border-radius: 5px;
+ ">{{ $spending->tag->name }}</div>
+ @endif
+ <div>{{ $spending->description }}</div>
+ <div style="margin-top: 10px; font-size: 14px;">{{ $spending->formatted_happened_on }}</div>
</div>
- @endforeach
+ <div class="row__column text-right" style="color: red;">&euro; {{ $spending->formatted_amount }}</div>
</div>
- <div class="spacing-top-large text-align-center">
- {!! ($currentPage > 1) ? '<a href="/spendings?page=' . ($currentPage - 1) . ($sort ? '&sort=' . $sort : '') . '">Previous</a> &middot;' : '' !!} Page {{ $currentPage }} of {{ $totalPages }} {!! ($currentPage < $totalPages) ? '&middot; <a href="/spendings?page=' . ($currentPage + 1) . ($sort ? '&sort=' . $sort : '') . '">Next</a>' : '' !!}
+ @endforeach
+ @else
+ <div class="section text-center">You don't have any spendings</div>
+ @endif
</div>
</div>
@endsection

Refactor spendings page to simply display list of spendings