F
Filament3mo ago
zemo

What is the best way to conditionally hide a tab in the ListResource page?

I have a resource called SubjectResource, where I have a number of tabs and each of it modifies the query in a certain way. Based on the user role, I need to hide most of the tabs. How do I accomplish that? I have installed the plugin "Shield" to handle roles. Thank you!
Solution:
->extraAttributes(['style' => auth()->user()->can('view_this') ? '' : 'display: none;'])
Jump to solution
11 Replies
toeknee
toeknee3mo ago
->visible(fn($record) => auth()->user()->can('view_whatever_tab')
zemo
zemoOP3mo ago
I had already tried that with ->hidden() Method Filament\Resources\Components\Tab::visible does not exist. In the meanwhile, I've resorted to creating the tab conditionally or not, but I'll wait in case there is a better approach.
Solution
toeknee
toeknee3mo ago
->extraAttributes(['style' => auth()->user()->can('view_this') ? '' : 'display: none;'])
zemo
zemoOP3mo ago
thank you! should I use your approach or mine, or both are equally ok? what I've done is simply create the tabs based on the fact that you have a role or not. example:
if($user->hasRole('Admin')) {
$tabs = ['all' => Tab::make('Tutti')
->badge($this->getModel()::count())
];
}
if($user->hasRole('Admin')) {
$tabs = ['all' => Tab::make('Tutti')
->badge($this->getModel()::count())
];
}
toeknee
toeknee3mo ago
Lookgs good
zemo
zemoOP3mo ago
on a similar note, I would like to have a filter pre-selected and disabled on a resource, depending on the user role. I know I can use default() on a SelectFilter. the issue is that the user can just remove the filter by clicking on the "x". I thought about using disabled() but it does not exist on SelectFilter. I thought about just modifying the resource query based on the fact that you have a role or not. any more insights? or a way to disable the filter. Thank you
toeknee
toeknee3mo ago
You can modify the query and ensure it it always applies at the query level Or add a scope and condition the scope
zemo
zemoOP3mo ago
done this, thanks ❤️ how can I create a table filter based on a condition (you guessed it, the condition is the same lol)?
toeknee
toeknee3mo ago
Same way? But with visible
Matthew
Matthew3mo ago
Be aware that when you use Html attributes to hide something, you're not 'hiding' it from execution. All that data is still be passed to the browser.
zemo
zemoOP3mo ago
oh wow I didn't think that'd work, thanks!! oh thanks :)) I'm going to stick with my approach then
Want results from more Discord servers?
Add your server