I've multiple pages, where a user can have multiple roles. But I cannot get pagewise roles of a user in Spatie Permission. How can I achieve Spatie Permission like below:
Page | Role | User |
------------------------
A | Admin | 1 |
B | Member | 1 |
A | Cashier| 2 |
With Spatie you set roles and permissions, after that within the page, you need to check for relevant permissions, that is what it's meant for. However, you can always extend Spatie with your own set of custom permission handling.
i.e. you can create a config file with a list of routes that each user has access to and then add a helper method.
config file:
return [
ROLE_ADMIN => [
'route-name1',
'route-name2',
'route-name3',
],
ROLE_MEMBER => ['route-name2'],
ROLE_CASHIER => [],
];
permission method:
hasPageAccess(string $role, string $routeName): bool
{
$permissions = Arr::get(config('my-page-permissions'), $role, []);
return !!array_search($routeName, $permissions);
}