*/ public static function for(Page $page, ?User $except = null): Collection { $ancestors = array_column(app(PageTree::class)->ancestors($page->id), 'id'); $roleIds = DB::table('grants') ->where(function ($q) use ($page, $ancestors) { $q->whereIn('ability', ['*', 'pages.manage'])->whereNull('resource_type'); $q->orWhere(fn ($q) => $q->where('ability', 'pages.publish')->where('resource_type', 'page')->where(fn ($q) => $q ->whereNull('resource_id') ->orWhere('resource_id', $page->id) ->orWhere(fn ($q) => $q->whereIn('resource_id', $ancestors ?: [0])->where('include_children', true)))); }) ->pluck('role_id'); return User::query()->active() ->whereIn('id', DB::table('role_user')->whereIn('role_id', $roleIds)->select('user_id')) ->when($except, fn ($q) => $q->whereKeyNot($except->id)) ->limit(50) ->get(); } }