F
Filamentβ€’13mo ago
Husky110

Filter gets applied automatically?

Hey again - sorry if my questions are a bit noob-like, I just try to get filament figured out... πŸ™‚ This time I have a weird behaviour. I want to show some articles in my table. For that my table actually uses a morphed-model which has an "article"-relationship. What I want: Show only articles where article.is_locked = 0 and make articles with article.is_locked = 1 only visible if a filter is active. I got so far to build a filter like this:
Tables\Filters\Filter::make('article.is_locked')
->query(function (Builder $query){
$query->whereHas('article', function ($subquery){
$subquery->where('is_locked', 1);
});
})
->label(__('articles.list.filters.is_locked'))
->toggle()
Tables\Filters\Filter::make('article.is_locked')
->query(function (Builder $query){
$query->whereHas('article', function ($subquery){
$subquery->where('is_locked', 1);
});
})
->label(__('articles.list.filters.is_locked'))
->toggle()
But this filter seems to get applied automatically, so right now I only see locked articles even tho the filter is not active. My two questions here are: Is this normal an intended? And secondly - I am sure I am missing something here, but I need help figuring out what exactly. Thanks. πŸ™‚
6 Replies
ZedoX
ZedoXβ€’13mo ago
Obtain $data and check for the value, and apply the filter only if the value in the data is true
->query(function (Builder $query, array $data) {
...
})
->query(function (Builder $query, array $data) {
...
})
Husky110
Husky110β€’13mo ago
So like this?
Tables\Filters\Filter::make('article.is_locked')
->query(function (Builder $query, array $data){
if($data){
$query->whereHas('article', function ($subquery){
$subquery->where('is_locked', 1);
});
}
})
->label(__('articles.list.filters.is_locked'))
->toggle()
Tables\Filters\Filter::make('article.is_locked')
->query(function (Builder $query, array $data){
if($data){
$query->whereHas('article', function ($subquery){
$subquery->where('is_locked', 1);
});
}
})
->label(__('articles.list.filters.is_locked'))
->toggle()
And does this cover the preamptive filtering (get me all data where article.is_locked = 0) aswell?
Vp
Vpβ€’13mo ago
At first you will get everything (lock == 0 or 1) if you didn't change the default model query.. If toggle then base on your query you should see the filter..
Majid Al Zariey
Majid Al Zarieyβ€’13mo ago
default false?
ZedoX
ZedoXβ€’13mo ago
@husky110 remove the . from the filter name, it seems like that is causing it to be always active Tables\Filters\Filter::make('is_locked')
Husky110
Husky110β€’13mo ago
And how do I change the default query? Didn't find it in the docs yesterday... Yep - Thank you. That removed the "always active"-part. πŸ™‚ Okay - My whole approach to this was wrong... After some more reading I was able to achieve what I wanted by using a TernaryFilter. I think that thing was designed to that specific use-case, but when you are new to this whole approach (never worked with a frontend-stack before) it just happens that the concepts are not clear from the get go. Thank you all for your time and efforts tho. πŸ™‚