F
Filament2mo 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
toeknee2mo ago
->visible(fn($record) => auth()->user()->can('view_whatever_tab')
zemo
zemo2mo 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
toeknee2mo ago
->extraAttributes(['style' => auth()->user()->can('view_this') ? '' : 'display: none;'])
zemo
zemo2mo 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
toeknee2mo ago
Lookgs good
zemo
zemo2mo 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
toeknee2mo 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
zemo2mo 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
toeknee5w ago
Same way? But with visible
Matthew
Matthew5w 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
zemo5w 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