@php use Illuminate\Support\Facades\Auth; use App\Models\Role; use App\Models\RoleAccess; use Spatie\Permission\Models\Permission; $staff = Auth::guard('staff')->user(); $staffPermissionIds = []; if ($staff) { $role = Role::where('role', $staff->designation)->first(); if ($role) { $allAccess = RoleAccess::where('role_id', $role->id) ->where('user_type', 'all') ->first(); $typeAccess = RoleAccess::where('role_id', $role->id) ->where('user_type', $staff->type) ->first(); $allPermissions = $allAccess?->permissions ?? []; $typePermissions = $typeAccess?->permissions ?? []; $staffPermissionIds = array_unique( array_map( 'intval', array_merge( $allPermissions, $typePermissions ) ) ); } } $can = function ($permissionName) use ($staffPermissionIds) { $permission = Permission::where('name', $permissionName) ->where('guard_name', 'staff') ->first(); if (!$permission) { return false; } return in_array( (int) $permission->id, $staffPermissionIds, true ); }; @endphp