Limiting what Roles are shown on vyuldashev/nova-permission

Published on Jul 28, 2021

While working on a project that used Nova to assign roles to users, I did not want a particular role to be part of the choices for the RoleBooleanGroup field.

Looking through the Issues of the repo on GitHub, I saw this one that described the same problem that I had. I checked out the source code of the RoleBooleanGroup from the package and saw that it did this in the constructor.

$options = $roleClass::get()->pluck($labelAttribute ?? 'name', 'name')->toArray();


It was fetching all roles from the database and building an associative array out of it then passes it all to the parent's options() method. I thought about extending the class and adding a new constructor option but it didn't feel right. Ended up doing this instead.

namespace App\Nova\Fields;

class RoleBooleanGroup extends \Vyuldashev\NovaPermission\RoleBooleanGroup
    public function withOptions(array $options): self

        return $this;

This was a better solution and it allowed me to do something as simple as this on my User Resource's fields() method.

                    'Agent' => 'Agent',
                    'Supervisor' => 'Supervisor',