insenseanalytics / laravel-nova-permission

A Laravel Nova tool for the Spatie Permission package
MIT License
78 stars 33 forks source link

Unable to attach records in one-to-many relationship #27

Closed mattpatera-ych closed 3 years ago

mattpatera-ych commented 3 years ago

Hi!

I have a relationship between two models, Customer and User, called Representatives. From the Customer resource, users should be able to attach/detach User resources. I have followed all of the steps for installation, and other permissions are working, but the button for attach is not appearing, despite the user having permissions to attach representatives. They DO however have the ability to detach representatives.

From my Customer nova resource:

use \Insenseanalytics\LaravelNovaPermission\PermissionsBasedAuthTrait;

public static $permissionsForAbilities = [
    ...
    'attachUser' => 'attach representatives to customers',
    'detachUser' => 'detach representatives from customers',
    ...
];

The relationship itself from the nova resource (there is a pivot table in between that has a secondary field with a foreign key specifying the representative type):

BelongsToMany::make('Representatives', 'users', User::class)
                ->fields(function () {
                    return [
                        Select::make('Rep Type', 'representative_type_id')
                            ->options(\App\Models\RepresentativeType::pluck('name', 'id')->toArray())
                            ->displayUsingLabels()
                    ];
                }),

And an image of what I'm actually seeing in the nova crud we have:

Screen Shot 2021-10-27 at 4 22 50 PM

If you have any suggestions or need any additional information, would be happy to provide it. I've tried everything I can think to try to get the button to appear.

Thanks!

mattpatera-ych commented 3 years ago

If anyone else happens to come across this, the issue was that attachAny{Model} needed to be also enabled via permissions on the resource.