RelationManager Attach and Detach possible values
I have RoleResource, with a PermissionRelationManager.
1. I'd like to have the table in RelationManager show all possible Permissions to attach to the role, whether attached or not, instead of just those currently attached.
2. I need to be able to programmatically determine whether it's attached or not, so I can conditionally show the Attach or Detach button.
If it helps, I have an array called $permissions which contains all the permission methods defined in each Policy that I can use, I'm just not sure how/where to bind that into the table or if that's not the right way of doing it, how to adjust to show all possible with an idea of whether they're Attached or not. Cheers!
14 Replies
Im assume you are trying to show all attached/detacted in a table and you click the button to attach/detact right?
If so.. you might be able to do it by rendering all permissions, then set a visible on attach if !$user->hasPermission('view X') and detached($user->hasPermission('view X')
Yes exactly that! So how do I render all permissions? The attach/detach would in this case need to be on $role->has but that should work too since it's an operator exposed by Spatie (which I'm using underneath ofc)
One minute
So this as the relationship manager table:
I haven't coded up the actions properly just free handed but should do what you want with some tinkering
something like that should work atleast
That's amazing! Thank you so much! In fairness I am fine to figure out the table actions, it's mostly this bit I was not sure about
$table
->modifyQueryUsing(fn ($query) => $query = Permission::query())
- does this simply point at the model Permissions and get them?Yep exactly, it removes the existing query which is scoped to the user and sets it as a fresh query
You could use ->query() but then you miss all the filters etc. So the above gives more control without relying on the relationship of the current model since they it scopes it too.
I'm struggling to combine it with tabs, which also overloads modifyQueryUsing... I will get there!
FYI in my case idc about the filters so I did just override the query, meaning the tabs subsequently modify it correctly \o/
Sorry to tag you, but I am really stuck on this, no matter what I do I cannot get the 'attach' action to even do the dd('hit') - it posts but nothing is shown. I can't make it log output or anything and I feel I must be doing something fundamentally wrong
the action can be tempramental depending on the code. I'll try and take a look
Can you pass in what you are using
Thanks for the offer, as I said the detch or deny action works great, the attach or permit action won't even do a dd
Ok so the attach/detach from where work?
The
Action::make('deny')
works and detaches the permission from the role
If I change Action::make('deny')
to perform givePermissionTo
it doesn't work which doesn't help much, but either way when I use the permit
button to run ->action(fn () => dd('hit')),
that doesn't even do the ddI\ve got a feeling it's down to the query
Bit more code then in case anything obvious jumps out
Wondering if it's better to just build a widget instead of hacking the relationship manager
At the moment I'm thinking it should have it's own Edit page where it simply displays what would otherwise be the owner Record as read only fields for visual consistency, and then uses a form with Forms\Components\CheckboxList, doing a bulk attach/detach. But I will loop back to that later