© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
12 replies
Falk Maria Zeitsprung

menu item depending on user role or permission

How can i set a menu item only, if the user has a specific role?

I tried https://filamentphp.com/tricks/conditions-on-user-menu-items-based-on-roles
But cant get it running.

I installed in config/filament.php
'auth' => [
            Authenticate::class,
            UserMenuItemMiddleware::class,
'auth' => [
            Authenticate::class,
            UserMenuItemMiddleware::class,
`

I created
php artisan make:middleware UserMenuItemMiddleware
php artisan make:middleware UserMenuItemMiddleware


public function handle(Request $request, Closure $next): Response
    {
        if (Auth::user()->hasRole('Root')) {

            Filament::serving(function () {

                Filament::registerUserMenuItems([
                    'GitLog' => UserMenuItem::make()
                        ->label('Git Log')
                        ->url(route('filament.pages.settings'))
                        ->icon('heroicon-s-cog'),

                ]);
            });
        }
public function handle(Request $request, Closure $next): Response
    {
        if (Auth::user()->hasRole('Root')) {

            Filament::serving(function () {

                Filament::registerUserMenuItems([
                    'GitLog' => UserMenuItem::make()
                        ->label('Git Log')
                        ->url(route('filament.pages.settings'))
                        ->icon('heroicon-s-cog'),

                ]);
            });
        }



I have a Page in app/Filament/Pages/GitLog.php with
class GitLog extends Page
{
    protected static ?string $title = 'Git Log';
    protected static ?string $navigationLabel = 'Git Log';
    protected static ?string $navigationIcon = 'heroicon-o-document-text';
    //protected static ?string $navigationGroup = 'Settings';
    //protected static ?string $navigationIcon = 'heroicon-o-beaker';
    protected static string $view = 'filament.pages.git-log';
    protected static ?int $navigationSort = 4;

    public $content;

    public function mount(): void
    {
        //abort_unless(auth()->user()->hasRole(['Root', 'Admin']), 403);
        abort_unless(auth()->user()->hasRole(['Root']), 403);
        $this->content = file_get_contents(base_path('git-log.txt'));

    }

}
class GitLog extends Page
{
    protected static ?string $title = 'Git Log';
    protected static ?string $navigationLabel = 'Git Log';
    protected static ?string $navigationIcon = 'heroicon-o-document-text';
    //protected static ?string $navigationGroup = 'Settings';
    //protected static ?string $navigationIcon = 'heroicon-o-beaker';
    protected static string $view = 'filament.pages.git-log';
    protected static ?int $navigationSort = 4;

    public $content;

    public function mount(): void
    {
        //abort_unless(auth()->user()->hasRole(['Root', 'Admin']), 403);
        abort_unless(auth()->user()->hasRole(['Root']), 403);
        $this->content = file_get_contents(base_path('git-log.txt'));

    }

}


What to i have to put in app/Http/Middleware/UserMenuItemMiddleware.php please?
I want to show the page GitLog.php only when the user has role "Root".
Filament
Conditions on user menu items based on roles by sheldon - Tricks - ...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Different log-in behaviour depending on user role
FilamentFFilament / ❓┊help
3y ago
Dropdown access depending on role
FilamentFFilament / ❓┊help
3y ago
Conditional hiding of user menu item
FilamentFFilament / ❓┊help
3y ago