Closed Umar-Farooq-Shafi closed 1 year ago
The constraints from the filter aren't applied to the relationship count.
Make sure to add the relationship query constraints to the counts()
method like so:
counts(['surgeries' => function (Builder $query) {
// ...
}])
@zepfietje I don't want to apply constraints on the counts()
. I want update the counts base on the date filter
why is this close? any solution or workaround? @zepfietje
You should access the active filters and apply the constraint to the counts()
method as I explained above. That's the solution. 🙂
sorry, what is the sentex to access the active filter?
Something along those lines:
function (Builder $query, HasTable $livewire) {
$livewire->getTableFilterState('created_at');
}
getting error
Too few arguments to function App\Filament\Resources\UserResource::App\Filament\Resources\
{closure}(), 1 passed in /Users/dev/Desktop/Personal/PMU/pmu-
ahp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 1603 and
exactly 2 expected
Tables\Columns\TextColumn::make('surgeries_count')
->label('Entries')
->counts(['surgeries' => function (Builder $query, HasTable $livewire) {
dd($livewire->getTableFilterState('created_from'));
}])
->sortable()
->toggleable(),
Could you share more context of the error using a Flare exception link?
https://flareapp.io/share/B5ZAWWp7 @zepfietje
Ah right, $livewire
is probably not injected there.
Since this question isn't really a bug, could you ask for help in the #help
channel in the Filament Discord server?
https://discord.gg/filament
I'm too busy working on finishing v3 at the moment, sorry.
I do not have permission to create a post. can you please help me?
click the button at the top to complete onboarding first
thanks @danharrin
@Umar-Farooq-Shafi did you find an answer for this?
Yes, I added the following function inside the ListRecord class
protected function getTableQuery(): Builder
{
if (is_null($this->tableFilters)) {
return parent::getTableQuery()->withCount('patients');
}
$data = data_get($this->tableFilters, 'date', []);
return parent::getTableQuery()->withCount([
'patients as patients_count' => function (Builder $builder) use ($data) {
$from = data_get($data, 'from');
$until = data_get($data, 'until');
if (! is_null($from) && $from !== 'null') {
$builder->where('patients.date', '>=', $from);
}
if (! is_null($until) && $until !== 'null') {
$builder->where('patients.date', '<=', $until);
}
return $builder;
},
])
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
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 UserResource and a column which is some of the entries of another table. I added the from and until filter in the table and when I try to update the counts, it's not updating.
Expected behavior
The
Entries
column should update the count according to the filter. here is the demo.Steps to reproduce
Add the from and until date filter in the filament resource table with the counts of relation 1:N table.
Reproduction repository
https://github.com/Umar-Farooq-Shafi/filament-filter-issue
Relevant log output
No response