3 namespace BookStack\Users\Queries;
5 use BookStack\Users\Models\Role;
6 use BookStack\Util\SimpleListOptions;
7 use Illuminate\Pagination\LengthAwarePaginator;
10 * Get all the roles in the system in a paginated format.
12 class RolesAllPaginatedAndSorted
14 public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator
16 $sort = $listOptions->getSort();
17 if ($sort === 'created_at') {
18 $sort = 'roles.created_at';
21 $query = Role::query()->select(['*'])
22 ->withCount(['users', 'permissions'])
23 ->orderBy($sort, $listOptions->getOrder());
25 if ($listOptions->getSearch()) {
26 $term = '%' . $listOptions->getSearch() . '%';
27 $query->where(function ($query) use ($term) {
28 $query->where('display_name', 'like', $term)
29 ->orWhere('description', 'like', $term);
33 return $query->paginate($count);