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

Kenneth_H's avatar

Role and permission listing

Hi I have now implemented Zizaco/Entrust in order to have effective role and permission enforcement in my application. I want to make table, like they have in Drupal, where I can see which permissions are assigned to each role. It is no problem to create a column for each role and place a checkbox in these columns next to a specific permission. My issue is that I cannot get it to assign a true/false value for each checkbox. If I did just have an array of strings or integers, I could easily use in_array() for this, but it does not work with an array of Eloquent models. Any suggestion for how I can determine ifa specific role has a given permission?

0 likes
2 replies
Ozan's avatar

You can copy the hasRole method from User model and adapt it to Role Model for permissions.

Kenneth_H's avatar
Kenneth_H
OP
Best Answer
Level 16

Ended up creating this method

public function permissions()
    {
        $permissionList = $this->perms;
        $output = array();
        foreach ($permissionList as $permission)
        {
            $output[] = $permission->id;
        }
        
        return $output;
    }

Now it returns an array of permission_id's which I can then compare the ID of the current permission using in_array().

Please or to participate in this conversation.