getRoles() and getAbilities() give a different result
nielseulink opened this issue · comments
Hi there, this is the first time I have to use an permission system in my app and went for bouncer. I made it possible to add the user to an role with an multiselect element and the sync function what is working fine (using livewire, and mount getRoles()
to the select element). attempted to try the same for roles (abilities to roles). Unfortunately this doesn't work because the result is completely different from the getRoles()
and getAbilities()
(see examples beneath). I think this is an issue, anyone that can confirm this ?
Code:
$user = App\Models\User::find(1);
$user->getRoles();
Result:
Illuminate\Support\Collection {#2368
all: [
"admin",
"support",
],
}
Code:
$role = Silber\Bouncer\Database\Role::find(1);
$role->getAbilities();
Result:
Illuminate\Database\Eloquent\Collection {#2389
all: [
Silber\Bouncer\Database\Ability {#2403
id: 1,
name: "*",
title: "All abilities",
entity_id: null,
entity_type: "*",
only_owned: 0,
options: null,
scope: null,
created_at: "2022-02-14 12:11:55",
updated_at: "2022-02-14 12:11:55",
},
],
}
If you only want the title, use the Collection
's pluck
method:
$role->getAbilities()->pluck('title');
You probably want the "key" to be the ability's ID. You can pass the desired key to pluck
as a second argument:
$role->getAbilities()->pluck('title', 'id');
If you only want the title, use the
Collection
'spluck
method:$role->getAbilities()->pluck('title');
You probably want the "key" to be the ability's ID. You can pass the desired key to
pluck
as a second argument:$role->getAbilities()->pluck('title', 'id');
this was indeed the solution for me. Thank you!