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

jufrensius's avatar

Retrieve permissions by selected role id in Spatie

Hi,

I'm using spatie for authorization and I have no idea to retrieve selected permissions by role id. The schema are described below:

roles table

  • id
  • name

permissions table

  • id
  • name

role_has_permissions table

  • permission_id
  • role_id

Can anyone give me an example by using eloquent ORM? Thank you in advance

0 likes
1 reply
Shaden's avatar

Spatie Role has a relation with Permissions

  /**
     * A role may be given various permissions.
     */
    public function permissions(): BelongsToMany
    {
        return $this->belongsToMany(
            config('permission.models.permission'),
            config('permission.table_names.role_has_permissions'),
            'role_id',
            'permission_id'
        );
    }

So you can just say

$permissioins = $role->permissions ;

Please or to participate in this conversation.