Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davfount's avatar

Spatie Laravel Permissions and Nova

Hello, I am currently trying to create resources for my Roles + Permissions. Due to the way nova handles the belongs to many field (creating a attach permissions section on the view page) it makes it very unpleasant to configure a newly created role.

What I'd like to do is maybe have a multi select or even check boxes. Without nova I accomplished this by creating check boxes and when they were selected they were added to an array. I was then able to sync the role with the new set of permissions on save.

However since Nova utilizes the default controllers for the update/store methods I'm not exactly sure how to accomplish this here. I don't have access to the controllers in that package. I have already extended the model classes to add activity logging.

Has anyone worked with something similar or have tips I can follow in order to accomplish this?

It wouldn't be an issue but there are well over 300 permissions that can be assigned and something tells me my customers would not be happy having to click Attach and add another 300 times to create an admin role lol

0 likes
3 replies
LaryAI's avatar
Level 58

One possible solution to this problem is to create a custom Nova field that allows for selecting multiple permissions at once. Here's an example of how you can achieve this:

  1. Create a new Nova field class by running the following command in your terminal:
php artisan nova:field PermissionsField
  1. Open the generated PermissionsField.php file and update the jsonSerialize method to return the necessary data for rendering the field. You can use a multi-select or checkboxes to allow for selecting multiple permissions. Here's an example using a multi-select:
public function jsonSerialize()
{
    return array_merge(parent::jsonSerialize(), [
        'options' => Permission::pluck('name', 'id'),
    ]);
}
  1. Next, update the resolve method to handle saving the selected permissions. You can use the attach method to sync the selected permissions with the role. Here's an example:
public function resolve($resource, $attribute = null)
{
    $this->value = $resource->permissions->pluck('id')->toArray();
}

public function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
    $model->permissions()->sync($request->input($requestAttribute));
}
  1. Finally, use the new PermissionsField in your Nova resource by adding it to the fields method. Here's an example:
use App\Nova\Fields\PermissionsField;

public function fields(Request $request)
{
    return [
        // other fields
        PermissionsField::make('Permissions'),
    ];
}

With this custom field, you can now select multiple permissions at once, making it easier to configure a newly created role in Nova.

Note: This solution assumes you are using the Spatie Laravel Permissions package and have a Permission model and a permissions relationship defined on your Role model. Adjust the code accordingly if your setup is different.

krisi_gjika's avatar

you can "extend" the nova create and update functionality by implementing model observers

davfount's avatar

@krisi_gjika Thanks for that suggestion. I was able to figure it out after a long talk with Larry. LOL I will definitely be using observables though for creating alerts on some of the various resources when they get created.

I ended up refining my google searches and I found a package that does exactly what I was looking for. I did have to fork it and fix the css but other than that its working great so far in all of my belongs to many relationships.

https://github.com/blendbyte/nova-attach-many

1 like

Please or to participate in this conversation.