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,
`

I created
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'),

                ]);
            });
        }



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'));

    }

}


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
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Was this page helpful?