Allow filtering by tag on transactions' index view
MODIFY
app/Http/Controllers/TransactionController.php
app/Http/Controllers/TransactionController.php
@@ -6,11 +6,20 @@ use Carbon\Carbon;
use Illuminate\Http\Request;
class TransactionController extends Controller {
- public function index() {
+ public function index(Request $request) {
+ $filterBy = $request->get('filterBy') ? explode('-', $request->get('filterBy')) : null;
+
$yearMonths = [];
// Populate yearMonths with earnings
foreach (session('space')->earnings as $earning) {
+ $shouldAdd = false;
+
+ if (!$filterBy) {
+ $shouldAdd = true;
+ }
+
+ if ($shouldAdd) {
$i = Carbon::parse($earning->happened_on)->format('Y-m');
if (!isset($yearMonths[$i])) {
@@ -19,9 +28,20 @@ class TransactionController extends Controller {
$yearMonths[$i][] = $earning;
}
+ }
// Populate yearMonths with spendings
foreach (session('space')->spendings as $spending) {
+ $shouldAdd = true;
+
+ // Filter
+ if ($filterBy[0] == 'tag') {
+ if (!$spending->tag || $spending->tag->id != $filterBy[1]) {
+ $shouldAdd = false;
+ }
+ }
+
+ if ($shouldAdd) {
$i = Carbon::parse($spending->happened_on)->format('Y-m');
if (!isset($yearMonths[$i])) {
@@ -30,6 +50,7 @@ class TransactionController extends Controller {
$yearMonths[$i][] = $spending;
}
+ }
// Sort transactions
foreach ($yearMonths as &$yearMonth) {
@@ -41,7 +62,9 @@ class TransactionController extends Controller {
// Sort yearMonths
krsort($yearMonths);
- return view('transactions.index', compact('yearMonths'));
+ $tags = session('space')->tags;
+
+ return view('transactions.index', compact('yearMonths', 'tags'));
}
public function create() {

MODIFY
resources/views/transactions/index.blade.php
resources/views/transactions/index.blade.php
@section('body')
<div class="wrapper my-3">
<h2>{{ __('models.transactions') }}</h2>
+ <div class="row mt-1">
+ <div class="row__column row__column--compact row__column--compact">Filter by Tag: </div>
+ <div class="row__column row__column--compact ml-1">
+ <a href="/transactions">None</a>
+ </div>
+ @foreach ($tags as $tag)
+ <div class="row__column row__column--compact ml-1">
+ <a href="/transactions?filterBy=tag-{{ $tag->id }}">{{ $tag->name }}</a>
+ </div>
+ @endforeach
+ </div>
@foreach ($yearMonths as $key => $transactions)
<h2 class="mt-3 mb-2">{{ $key }}</h2>
<div class="box">

Allow filtering by tag on transactions' index view