Resource Filter on counts relationship

I've a table and Date range filter and want to apply the filter on the counts. I opened an issue on the Github. Please take a look for further information. thanks https://github.com/filamentphp/filament/issues/7187
GitHub
FilamentPHP filter not updating the record count · Issue #7187 · fi...
Package filament/filament Package Version 2.0 Laravel Version 10 Livewire Version 2.11 PHP Version 8.1 Problem description The Laravel FilamentPHP filter query not updating the records. I've Us...
Solution:
I think you can customize the eloquent query on the list page: https://filamentphp.com/docs/2.x/admin/resources/listing-records#customizing-the-eloquent-query ```php...
Filament
Listing records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
Jump to solution
2 Replies
Solution
LeandroFerreira
LeandroFerreira17mo ago
I think you can customize the eloquent query on the list page: https://filamentphp.com/docs/2.x/admin/resources/listing-records#customizing-the-eloquent-query
protected function getTableQuery(): Builder
{
$createdFrom = $this->tableFilters['created_at']['created_from'] ?? null;
$createdUntil = $this->tableFilters['created_at']['created_until'] ?? null;

return parent::getTableQuery()
->withCount(['surgeries', 'surgeries as surgeries_count' => function (Builder $query) use ($createdFrom, $createdUntil) {
return $query
->when(
$createdFrom,
fn (Builder $query, $date): Builder => $query->whereDate('surgeries.date', '>=', $date),
)
->when(
$createdUntil,
fn (Builder $query, $date): Builder => $query->whereDate('surgeries.date', '<=', $date),
);
}]);
}
protected function getTableQuery(): Builder
{
$createdFrom = $this->tableFilters['created_at']['created_from'] ?? null;
$createdUntil = $this->tableFilters['created_at']['created_until'] ?? null;

return parent::getTableQuery()
->withCount(['surgeries', 'surgeries as surgeries_count' => function (Builder $query) use ($createdFrom, $createdUntil) {
return $query
->when(
$createdFrom,
fn (Builder $query, $date): Builder => $query->whereDate('surgeries.date', '>=', $date),
)
->when(
$createdUntil,
fn (Builder $query, $date): Builder => $query->whereDate('surgeries.date', '<=', $date),
);
}]);
}
resource column
Tables\Columns\TextColumn::make('surgeries_count')
->label('Entries')
//->counts('surgeries')
->sortable()
->toggleable()
Tables\Columns\TextColumn::make('surgeries_count')
->label('Entries')
//->counts('surgeries')
->sortable()
->toggleable()
resource filter
...
->filters([
Filter::make('created_at')
->form([
Forms\Components\DatePicker::make('created_from'),
Forms\Components\DatePicker::make('created_until')
])
->query(fn (Builder $query, array $data): Builder => $query)

])
...
->filters([
Filter::make('created_at')
->form([
Forms\Components\DatePicker::make('created_from'),
Forms\Components\DatePicker::make('created_until')
])
->query(fn (Builder $query, array $data): Builder => $query)

])
Filament
Listing records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
Umar Farooq
Umar FarooqOP17mo ago
thanks @Leandro Ferreira . thats' solve my problem
Want results from more Discord servers?
Add your server