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:Jump to solution
->extraAttributes(['style' => auth()->user()->can('view_this') ? '' : 'display: none;'])
11 Replies
->visible(fn($record) => auth()->user()->can('view_whatever_tab')
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
->extraAttributes(['style' => auth()->user()->can('view_this') ? '' : 'display: none;'])
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:
Lookgs good
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
You can modify the query and ensure it it always applies at the query level
Or add a scope and condition the scope
done this, thanks ❤️
how can I create a table filter based on a condition (you guessed it, the condition is the same lol)?
Same way? But with visible
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.
oh wow I didn't think that'd work, thanks!!
oh thanks :)) I'm going to stick with my approach then