Skip to content

Commit c94082c

Browse files
committed
feat: add transaction filter
1 parent 335f16d commit c94082c

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

app/Filament/Resources/TransactionResource.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Filament\Resources\Resource;
1414
use Filament\Tables;
1515
use Filament\Tables\Table;
16+
use Filament\Tables\Filters\SelectFilter;
1617
use Filament\Tables\Filters\Filter;
1718

1819
class TransactionResource extends Resource
@@ -68,6 +69,7 @@ public static function table(Table $table): Table
6869
->columns([
6970
Tables\Columns\TextColumn::make('category.name')
7071
->label('Category')
72+
->searchable()
7173
->formatStateUsing(
7274
fn($state, $record) =>
7375
'<div style="display: flex; align-items: center; gap: 0.5rem;">' .
@@ -96,6 +98,7 @@ public static function table(Table $table): Table
9698
->toggleable(isToggledHiddenByDefault: true),
9799
Tables\Columns\TextColumn::make('amount')
98100
->label('Amount')
101+
->searchable()
99102
->sortable()
100103
->formatStateUsing(function ($state) {
101104
static $currency = null;
@@ -112,6 +115,7 @@ public static function table(Table $table): Table
112115
->formatStateUsing(fn($state) => Carbon::parse($state)->format('d M Y H:i')),
113116
Tables\Columns\TextColumn::make('description')
114117
->label('Description')
118+
->searchable()
115119
->limit(25)
116120
->tooltip(fn($record) => $record->description),
117121
Tables\Columns\TextColumn::make('created_at')
@@ -126,14 +130,39 @@ public static function table(Table $table): Table
126130
->filters([
127131
Filter::make('date_time')
128132
->form([
129-
Forms\Components\DatePicker::make('from'),
130-
Forms\Components\DatePicker::make('until'),
133+
Forms\Components\DatePicker::make('from')->label('From'),
134+
Forms\Components\DatePicker::make('until')->label('Until'),
131135
])
136+
->columns(2)
132137
->query(function ($query, array $data) {
133138
return $query
134139
->when($data['from'], fn($q) => $q->whereDate('date_time', '>=', $data['from']))
135140
->when($data['until'], fn($q) => $q->whereDate('date_time', '<=', $data['until']));
136141
}),
142+
SelectFilter::make('type')
143+
->options([
144+
'income' => 'Income',
145+
'expense' => 'Expense',
146+
])
147+
->label('Type'),
148+
149+
SelectFilter::make('category_id')
150+
->label('Category')
151+
->options(fn() => Category::all()->pluck('name', 'id'))
152+
->searchable(),
153+
154+
Filter::make('amount_range')
155+
->label('Amount Range')
156+
->form([
157+
Forms\Components\TextInput::make('min')->numeric()->label('Min'),
158+
Forms\Components\TextInput::make('max')->numeric()->label('Max'),
159+
])
160+
->columns(2)
161+
->query(function ($query, array $data) {
162+
return $query
163+
->when($data['min'], fn($q) => $q->where('amount', '>=', $data['min']))
164+
->when($data['max'], fn($q) => $q->where('amount', '<=', $data['max']));
165+
}),
137166
])
138167
->actions([
139168
Tables\Actions\ViewAction::make(),

0 commit comments

Comments
 (0)