F
Filamentβ€’14mo ago
MRBUG

Custom filter forms (From - Until Filter)

i am creating a filter for checking the data from the selected date to until selected date. And in documentation we have The custom filter It's cool. but I have another condition the date I want to filter is coming from another relation. OK my current resource name is Transaction. so I have a relation named payment and I am showing it's one field in transaction resource named payment_date so i can make a filter using documentation for transaction created_at column. like that
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

Filter::make('created_at')
->form([
DatePicker::make('created_from'),
DatePicker::make('created_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
})
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

Filter::make('created_at')
->form([
DatePicker::make('created_from'),
DatePicker::make('created_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
})
but i have a relation named payment and i want to filter its field payment_date in Transaction resource BTW i used this code
Filter::make('payment_date')
->form([
DatePicker::make('payment_date_from'),
DatePicker::make('payment_date_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['payment_date_from'],
fn (Builder $query, $date): Builder => $query->whereDate('project.payment_date', '>=', $date),)
->when(
$data['payment_date_until'],
fn (Builder $query, $date): Builder => $query->whereDate('project.payment_date', '<=', $date),);
})
Filter::make('payment_date')
->form([
DatePicker::make('payment_date_from'),
DatePicker::make('payment_date_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['payment_date_from'],
fn (Builder $query, $date): Builder => $query->whereDate('project.payment_date', '>=', $date),)
->when(
$data['payment_date_until'],
fn (Builder $query, $date): Builder => $query->whereDate('project.payment_date', '<=', $date),);
})
so how can i do that? THANKS IN ADVANCE.
Solution:
πŸ‘
Jump to solution
3 Replies
krekas
krekasβ€’14mo ago
because it's a relation i think you can use whereRelation() method https://laravel.com/docs/10.x/eloquent-relationships#inline-relationship-existence-queries
MRBUG
MRBUGOPβ€’14mo ago
ok thank you i will check it/ Thanks @krekas Its working
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['payment_date_from'],
fn (Builder $query, $date): Builder => $query->whereRelation('payment', 'payment_date', '>=', $date),
)
->when(
$data['payment_date_until'],
fn (Builder $query, $date): Builder => $query->whereRelation('payment', 'payment_date', '<=', $date),
);
})
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['payment_date_from'],
fn (Builder $query, $date): Builder => $query->whereRelation('payment', 'payment_date', '>=', $date),
)
->when(
$data['payment_date_until'],
fn (Builder $query, $date): Builder => $query->whereRelation('payment', 'payment_date', '<=', $date),
);
})
I used this code
Solution
krekas
krekasβ€’14mo ago
πŸ‘
Want results from more Discord servers?
Add your server