php - Spatie Permission: Multiple Roles on Multiple Pages in Laravel
Get the solution ↓↓↓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 |
Answer
Solution:
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);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.