Sunday, 11 December 2022

Spatie Laravel Permission & Check Role




$role = Role::findById(3, null); //use spatie\permissions\model\role to get role model;

$role->hasPermissionTo('Complaint Review'); //check role has the valid permission?


auth()->user()->hasPermissionTo('Complaint List'); //check under model user has permission ?

auth()->user()->permissions->pluck('name'); //getting all permissions plucn 'name' under model user

auth()->user()->getAllPermissions()->pluck('name'); //get all permissions name under (role) 


auth()->user()->roles->pluck('name'); //get model user role name

auth()->user()->roles->first()->name; //get role name under user

auth()->user()->roles->first()->name; //get role id under user


auth()->user()->can('Complaint Review'); //check under model_has_permission  

auth()->user()->getPermissionsViaRoles(); //get all permissions collection under role

foreach (auth()->user()->getPermissionsViaRoles() as $permission) {

  echo $permission->name . ' | ';

No comments:

Post a Comment