Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kenneth_H's avatar

Roles and permissions

Hi I have been looking for a package that would help me enforcing permissions on user roles in my laravel 5 project. I came across this package in my search: https://github.com/Zizaco/entrust/tree/laravel-5 It semmes to be just what I am looking for in terms of how I want to run the check, but the way relations are done is not exactly what I want. In the documentation it says that a user can have many roles and roles can be assigned to many users. As my system will be based the roles having all permissions I will only assign one role for each user. The roles will the have multiple permissions, but the same permission can be assigned to multiple roles.

From the documentation it seems like this package can do, but it does not tell how to check if a user has a specific permission based on their role. Can this package handle this setup or will I need to make my own package for this?

0 likes
3 replies
martinbean's avatar

@Kenneth_H If a user will only ever have one role, then you can just do permission checking in your code:

$user->can(['edit-user', 'create-post']);
1 like
ghvinashvili's avatar
Level 2
Entrust::hasRole('role-name');
Entrust::can('permission-name');

// is identical to

Auth::user()->hasRole('role-name');
Auth::user()->can('permission-name);

if your role name is 'owner' using look like that.

if(Entrust::hasRole('owner')) {
    //Do something
}

P.S. if you will create permission or roles dont use "-". use "_". if you will use "-", you will have problems.

Kenneth_H's avatar

Seems to be the solution. So in order to check if a certain user has a role that is granted sufficient permissions I would do something like this in my controller methods:

if (Auth::user()->can('view_admin_dashboard'))
{
 //do something
}

Please or to participate in this conversation.