user(); $checkPermission = $checkRole = false; if (auth()->check()) { // check if the spatie plugin functions exist $checkPermission = method_exists($user, 'hasAnyPermission'); $checkRole = method_exists($user, 'hasAnyRole'); } foreach ($array as $key => &$value) { if (is_callable($value)) { continue; } if ($checkPermission && isset($value['permission']) && !$user->hasAnyPermission((array) $value['permission'])) { unset($array[$key]); } if ($checkRole && isset($value['role']) && !$user->hasAnyRole((array) $value['role'])) { unset($array[$key]); } if (is_array($value)) { self::filterMenuPermissions($value); } } } }